strncmp gives 0 even when strings are NOT equal - C

1000 Views Asked by At

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'. Do you happen to know whether

Am I missing something in the strncmp function or somewhere else in my code?

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;
}
2

There are 2 best solutions below

0
Vlad from Moscow On BEST ANSWER

The thing is that it compares R with RUN and it gives 0. I want it to return 0 when it finds R only.

In this case you need to compare whole words using the function strcmp instead of comparing only lenwordtosearch characters using the function strncmp.

Take into account that the function fgets can append the new line character '\n' to the entered string. 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 ( 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);
    //...    
4
paxdiablo On
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.

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.

You should probably be checking the entire word. That probably means cleaning up the word you've read in from the file (i.e., removing newline) and using strcmp() instead, something like:

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