將一篇文本中的單詞保存在一個字符串數組中

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

    }

相關文章
相關標籤/搜索