Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.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 strok没有正确地标记单词_C_Token - Fatal编程技术网

C strok没有正确地标记单词

C strok没有正确地标记单词,c,token,C,Token,我正在尝试对单独的单词进行标记并将其存储到数组中,但由于某些原因,第一个单词存储在索引0中,其余单词未进行标记,全部存储在索引1中。 我有以下代码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char const *argv[]) { char input[300]; wh

我正在尝试对单独的单词进行标记并将其存储到数组中,但由于某些原因,第一个单词存储在索引0中,其余单词未进行标记,全部存储在索引1中。 我有以下代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char const *argv[])
{
   
    char input[300];

    while (1)
    {
       
        printf ("\nEnter input to check or q to quit\n");
        fgets(input, 300, stdin);

        for (int j = 0; j < 300; j++)
        {
            input[j] = tolower(input[j]);
        }

        /* remove the newline character from the input */
        int i = 0;
        while (input[i] != '\n' && input[i] != '\0')
        {
            i++;
        }
        input[i] = '\0';


        /* check if user enter q or Q to quit program */
        if ( (strcmp (input, "q") == 0) || (strcmp (input, "Q") == 0) )
            break;

        //printf ("%s\n", input);
        /*Start tokenizing the input into words separated by space
        *The tokenized words are added to an array of words*/

        char delim[] = " ";
        char *ptr = strtok(input, delim);
        int j = 0 ;

        
        // allocate our array
        char *wordList[15];
        for (i = 0 ; i < 16; i++)
        {
            wordList[i] = (char *) malloc(sizeof(char) * 300);
        }

        while (ptr != NULL)
        {
            strcpy(wordList[j], ptr);
            printf ("%s\n", wordList[j]);
            ptr = strtok(NULL, delim);
            j++;
        }
        printf ("%s\n", wordList[1]);
    }

    printf ("\nGoodbye\n");

    return 0;
}
我得到的输出

hello 
there sir
非常感谢您的帮助。谢谢。

这个循环

        // allocate our array
        char *wordList[15];
        for (i = 0 ; i < 16; i++)
        {
            wordList[i] = (char *) malloc(sizeof(char) * 300);
        }
或者减少迭代次数

        // allocate our array
        char *wordList[15];
        for (i = 0 ; i < 15; i++)
        {
            wordList[i] = (char *) malloc(sizeof(char) * 300);
        }
//分配我们的数组
字符*字表[15];
对于(i=0;i<15;i++)
{
单词表[i]=(字符*)malloc(字符大小)*300;
}
此循环

        // allocate our array
        char *wordList[15];
        for (i = 0 ; i < 16; i++)
        {
            wordList[i] = (char *) malloc(sizeof(char) * 300);
        }
或者减少迭代次数

        // allocate our array
        char *wordList[15];
        for (i = 0 ; i < 15; i++)
        {
            wordList[i] = (char *) malloc(sizeof(char) * 300);
        }
//分配我们的数组
字符*字表[15];
对于(i=0;i<15;i++)
{
单词表[i]=(字符*)malloc(字符大小)*300;
}

是否启用了所有编译器警告并修复了警告?如果没有,请更新你的代码+结果/不喜欢各种神奇数字-300,15-人们使用常量变量是有原因的,需要重复引用仅在一个位置定义的常量是有原因的。为了便于讨论,假设用户实际输入了fgets调用允许的300个字符,存储字符串需要多少字节?因此定义一个常量int stringlength=300;然后将数组大小调整为stringlength+1,以允许终止“\0”,并在for循环中使用stringlength。现在,如果更改字符串长度,您只有一个地方可以更改。是否启用了所有编译器警告并修复了警告?如果没有,请更新你的代码+结果/不喜欢各种神奇数字-300,15-人们使用常量变量是有原因的,需要重复引用仅在一个位置定义的常量是有原因的。为了便于讨论,假设用户实际输入了fgets调用允许的300个字符,存储字符串需要多少字节?因此定义一个常量int stringlength=300;然后将数组大小调整为stringlength+1,以允许终止“\0”,并在for循环中使用stringlength。现在,如果您更改字符串长度,您只有一个地方可以更改。代码不应该使用15/16-这些应该定义为常量,然后添加/减去数字。@barny“添加/减去的数字”是什么意思?无论如何,我认为在这里使用
300
而不是
15
16
更好,因为输入最多300个字符,所以令牌不会超过300个。然后将所有
300
s替换为宏将使代码更好然后使用ntokens调整单词列表的大小,for循环使用ntokens。尽量避免使用宏,因为宏的可维护性较差。代码不应使用15/16-这些宏应定义为常量,然后添加/减去数字。@barny“添加/减去数字”是什么意思?无论如何,我认为在这里使用
300
而不是
15
16
更好,因为输入最多300个字符,所以令牌不会超过300个。然后将所有
300
s替换为宏将使代码更好然后使用ntokens调整单词列表的大小,for循环使用ntokens。尽可能避免使用宏,因为宏的可维护性较差。