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

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

    char * strs[255] = {0};

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

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

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


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

            count++;

        }

        else{

            char * str = malloc(count + 1);

            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]);

    }


你可能感兴趣的:(count)