为什么我的程序打印出的次数比预期的多?

l00*_*ake -1 c strcmp

我有一个程序读取文件,逐行检查,并逐字分解.我的问题是,我将把每个单词存储在一个数组中,但是我需要使用strcmp函数来验证这个单词是否已经存在.无论如何,下面是我的代码,我的问题是,为什么我的程序打印出1这么多次?我期待它只打印两次因为this在我的文本文件中出现两次.

while (fgets(line, sizeof(line), fi) != NULL) { // looping through each line

    line_count += 1;

    for (j = 0; j < sizeof(line); j++) { // convert all words to lowercase
        line[j] = tolower(line[j]);
    }

    result = strtok(line, delimiters);

    while (result != NULL) {
        word_count += 1;

        if (strcmp(result, "this")) {
            printf("1\n");
        }

        result = strtok(NULL, delimiters); // get the next token
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是我的文字文件:

This is the first test.
This is the second test.
Run Code Online (Sandbox Code Playgroud)

ale*_*lex 7

strcmp()0如果字符串匹配则返回.你正在检查一个真正的价值.你真的想要strcmp(result, "this") == 0.

您还需要使匹配大小写不敏感,通常称为stricmp().