将一篇文本中的单词保存在一个字符串数组中

 char string[] = "I love ios I want to make an project zpp lanou";ios

    char * strs[255] = {0};ide

    int i = 0,count = 0; int wordcount = 0;string

    while (*(string + i) != '\0') {it

        char temp[100] = {0};int k = 0;static int m = 0;io


        if (*(string + i)!= ' ') {class

            count++;循环

        }static

        else{word

            char * str = malloc(count + 1);di

            wordcount++;

            for (int j = i - count;j < i;j++) {

                *(temp + k) = *(string + j);

                k++;

            }

            strcpy(str, temp);

            strs[m] = str;

            m++;

            count = 0;

        }

        if (*(string + i + 1) == '\0') {

            char * str = malloc(count + 1);k = 0;

            wordcount++;

            for (int j = i - count;j <= i;j++) {

                *(temp + k) = *(string + j);

                k++;

            }

            strcpy(str, temp);

            strs[m] = str;

            free(str);

            str = NULL;

        }

        i++;  ///!!!!!!!!!!!!!    在while循环必定不要忘了!!!!!!!!!!!!!!!!!

    }

    for (int i = 0; i < wordcount; i++) {

        printf("%s\t", strs[i]);

    }

相关文章
相关标签/搜索