Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 打印此数组的最简单方法_C_Arrays - Fatal编程技术网

C 打印此数组的最简单方法

C 打印此数组的最简单方法,c,arrays,C,Arrays,此代码的作用只是将一个句子分解为单个单词,例如:您输入我的名字是John,它返回: 我的 名字 是 约翰 我想知道有没有更好的方法重写这个 int main() { int w_size = 0; bool check_bool = false; char l_str[81]; char *ptr_to_word[81]; for (char *res_p = &(l_str[0]); *res_p != '\0'; res_p++) {

此代码的作用只是将一个句子分解为单个单词,例如:您输入
我的名字是John
,它返回:

  • 我的
  • 名字
  • 约翰
  • 我想知道有没有更好的方法重写这个

    int main() {
        int w_size = 0;
        bool check_bool = false;
        char l_str[81];
        char *ptr_to_word[81];
    
        for (char *res_p = &(l_str[0]); *res_p != '\0'; res_p++) {
            if ((*res_p != '.') && (*res_p != ',') && (*res_p != ' ') && (check_bool == false)) {
                ptr_to_word[w_size] = res_p;
                w_size++;
                check_bool = true;
            }
            if (((*res_p == '.') || (*res_p == ',') || (*res_p == ' ')) && (check_bool == true)) {
                check_bool = false;
            }
        }
    
        if (w_size == 0) {
            printf("no solution");
        } else {
            for (int i = 0; i < w_size; i++) {
                char *a = ptr_to_word[i];
                while ((*a != ',') && (*a != '.') && (*a != '\0') && (*a != ' ')) {
                    printf("%c", *a);
                    a++;
                }
                printf("\n");
            }
        }
        return 0;
    }
    
    intmain(){
    int w_size=0;
    bool check_bool=false;
    charl_str[81];
    char*ptr_to_单词[81];
    对于(char*res_p=&(l_str[0]);*res_p!='\0';res_p++){
    如果((*res_p!=')&&(*res_p!=',')&(*res_p!=')&&&(检查布尔==假)){
    ptr_to_word[w_size]=res_p;
    w_size++;
    检查_bool=true;
    }
    如果((*res_p='.)| |(*res_p='.','))| |(*res_p='')&&(检查布尔==true)){
    检查\u bool=false;
    }
    }
    如果(w_size==0){
    printf(“无解决方案”);
    }否则{
    对于(int i=0;i
    以下建议的代码:

    Please enter a sentence to be divided into words
    This is a sentence to be divided into words
    1: This
    
    2: is
    
    3: a
    
    4: sentence
    
    5: to
    
    6: be
    
    7: divided
    
    8: into
    
    9: words
    
  • 提示用户将句子分成几个单词
  • 干净地编译
  • 执行所需的功能
  • 现在,建议的代码:(按chqrlie编辑)


    如果不需要将单词存储到数组中,可以直接输出:

    #包括
    #包括
    int main(){
    char-str[81];
    printf(“输入字符串:”);
    如果(fgets(str,sizeof str,stdin)){
    int pos=0,len,index=1;
    对于(;;){
    /*跳过初始分离器*/
    pos+=strspn(str+pos,“,。\n”);
    如果(str[pos]='\0')
    打破
    /*计算单词的长度*/
    len=strcspn(str+pos,“,.\n”);
    printf(“%d:%.*s\n”,index++,len,str+pos);
    pos+=len;
    }
    }
    返回0;
    }
    
    查找
    strtok
    函数,它将帮助您实现此目标。这是否回答了您的问题?发布的代码是如何获得要拆分为单词的句子的?用户使用指针和创建的函数@user3629249输入句子@user3629249关于:
    for(size\t i=0;words[i];i++)
    单词[]的数组初始化为所有空值,因此()语句将导致循环在遇到
    words[]
    中包含null的条目时退出。是的,这是可行的,但不包括大写字母,有没有解决方法?而且它不需要输入标点符号,因为它会跳过标点符号,是否可以将所有标点符号都作为输入,然后在打印输出时跳过?谢谢你的邀请solution@chqrlieforyellowblockquotes,我希望你一开始就这么说。我将修改我的答案。
    Please enter a sentence to be divided into words
    This is a sentence to be divided into words
    1: This
    
    2: is
    
    3: a
    
    4: sentence
    
    5: to
    
    6: be
    
    7: divided
    
    8: into
    
    9: words