Search code examples
carraysstringstrcmp

C - strcmp() two strings from arrays


I am comparing two strings each coming from arrays.

while(countX<10){
    if(strcmp(scanWord[countX], currentWord[countX]) == 0)
        {scoreCurrent++;scoreCurrent++;}
    countX++;
}

"scanWord[countX]" and "currentWord[countX]" don't compare; each time coming up that they aren't the same even if they are. It works if I compare things that aren't those and I have printed them to screen to check too. They just don't seem to play well. Thanks in advance.


Solution

  • When you're reading the line, remove the newline:

    char *line = fgets(currentWord[countX], 20, stdin);
    if (line) {
        int len = strlen(line);
        if (line[len-1] == '\n') {
            line[len-1] = 0;
        }
    }