简体   繁体   English

即使字符串不相等,strncmp 也会返回 0 - C

[英]strncmp gives 0 even when strings are NOT equal - C

I am having a situation with strncmp function in C, it is returning 0 even when the words do not match, in the example below, I am testing it with the letter 'R' and when running the code it returns 0 even when the compared word in the txt document is 'RUN'.我在strncmp中遇到 strncmp function 的情况,即使单词不匹配,它也会返回 0,在下面的示例中,我正在用字母“R”测试它,运行代码时它返回 0,即使比较txt 文档中的单词是“RUN”。 Do you happen to know whether你是否碰巧知道

Am I missing something in the strncmp function or somewhere else in my code?我是否在 strncmp function 或我的代码中的其他地方遗漏了什么?

Thank you for your input.谢谢您的意见。


bool lookup(string s);

int main(void) {

char *s;
s = "R";
if (lookup(s)) {
    printf("Word found =)\n");
} else {
    printf("Word not found =(\n");
}
}

// Looks up word, s, in txt document.
bool lookup(string s)
{
 // TODO
    char *wordtosearch;
    wordtosearch = s;
    int lenwordtosearch = strlen(wordtosearch);
    char arraywordindic[50];

// Open txt file
FILE *file = fopen("text.txt", "r");
if (file == NULL)
{
    printf("Cannot open file, please try again...\n");
    return false;
}

while (!feof(file)) {
    if (fgets(arraywordindic, 50, file) != NULL) {
        char *wordindic;
        wordindic = arraywordindic;
        int result = strncmp(wordindic, wordtosearch, lenwordtosearch);
        if (result == 0) {
            printf("%i\n", result);
            printf("%s\n", wordindic);
            printf("%s\n", wordtosearch);
            fclose(file);
            return true;
        }
    }        
}
fclose(file);
return false;
}
int result = strncmp(wordindic, wordtosearch, lenwordtosearch);

This is going to give you zero if the first lenwordtosearch characters of wordtosearch matches the first lenwordtosearch characters of any word in the dictionary.如果wordtosearch的第一个lenwordtosearch字符与字典中任何单词的第一个lenwordtosearch字符匹配,这将为您提供零。

Given that the word you're searching for is S , any word in the dictioanary that starts with S is going to give you a match.鉴于您要搜索的词是S ,字典中以S开头的任何词都会为您提供匹配项。

You should probably be checking the entire word.您可能应该检查整个单词。 That probably means cleaning up the word you've read in from the file (ie, removing newline) and using strcmp() instead, something like:这可能意味着清理您从文件中读入的单词(即删除换行符)并改用strcmp() ,例如:

wordindic = arraywordindic;

// Add this:
size_t sz = strlen(wordindic);
if (sz > 0 && wordindic[sz - 1] == '\n')
    wordindic[sz - 1] = '\0';

// Modify this:
// int result = strncmp(wordindic, wordtosearch, lenwordtosearch);
int result = strcmp(wordindic, wordtosearch);

The thing is that it compares R with RUN and it gives 0. I want it to return 0 when it finds R only.问题是它将 R 与 RUN 进行比较,结果为 0。我希望它仅在找到 R 时返回 0。

In this case you need to compare whole words using the function strcmp instead of comparing only lenwordtosearch characters using the function strncmp .在这种情况下,您需要使用 function strcmp比较整个单词,而不是使用 function strncmp仅比较lenwordtosearch字符。

Take into account that the function fgets can append the new line character '\n' to the entered string.考虑到 function fgets可以 append 换行符'\n'到输入的字符串。 You need to remove it before comparing strings.您需要在比较字符串之前将其删除。

if (fgets(arraywordindic, 50, file) != NULL) {
    arraywordindic[ strcspn( arraywordindic, "\n" ) ] = '\0';
    int result = strcmp(arraywordindic, wordtosearch);
    if (result == 0) {
        printf("%i\n", result);
        printf("%s\n", arraywordindic);
        printf("%s\n", wordtosearch);

As a result these declarations结果这些声明

int lenwordtosearch = strlen(wordtosearch);

and

char *wordindic;
wordindic = arraywordindic

may be removed.可能会被删除。

And the condition of the while loop should be written like而 while 循环的条件应该写成

while ( fgets(arraywordindic, 50, file) != NULL ) {
    arraywordindic[ strcspn( arraywordindic, "\n" ) ] = '\0';
    int result = strcmp(arraywordindic, wordtosearch);
    if (result == 0) {
        printf("%i\n", result);
        printf("%s\n", arraywordindic);
        printf("%s\n", wordtosearch);
    //...    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM