0

I am new to C and am looking to write a program that checks if a word that a user enters is a legit word. I've scoured stackoverflow for suggestions but many are very specific to a particular case. Please before I get flamed, I am aware that this syntax is not correct but looking for some advice on how to fix it. My main problem is how to tell program to check the next line (after the "\0"). And perhaps the while condition is not the best way to tell C check until no more lines exist. In the below code I have sample compare string of "userword" which I use to test but ultimately this will be variable from user input.

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

int main (void)
{
    FILE *fp;
    fp = fopen("/usr/share/dict/words", "r");
    char line[50];
    while(fgets(line,50,fp) != NULL)
    {
        if(!strcpy(line,"userword"))
        {
            printf("Word exists\n");
            return 0; 
        }
    }
    printf("Word not found, try again.\n");
    return 0;
}
Andy
  • 275
  • 2
  • 14
  • Intuitively it seems like I need another loop nested within the 'while' loop which checks the next string in the file after each "\0" is found. I can probably figure out how to look for that ending character but not sure how I go to the next line. Or does C view this file as one consecutive string so I only need to check chars in between each "\0" ? – Andy Mar 28 '14 at 15:37
  • 3
    `strcpy()`? you mean `strcmp()`? – jfly Mar 28 '14 at 15:41
  • @jfly ugh yes. I knew I should not do this with a 102 fever. Thanks much dude – Andy Mar 28 '14 at 15:56
  • The specific problem being solved, in the longer-term, you might want to grab a copy of Jon Bentley's *Programming Pearls*; the first edition has a chapter that walks through this process and describes going from prototype to a usable program. Also, the field of "stemming" is useful for words that would be a waste to store in the dictionary. – John C Mar 28 '14 at 18:40

2 Answers2

3

strcpy() copies a string. You should use strcmp() which compares two strings.

Second, when you read the line from /usr/share/dict/words, there is a newline at the end. You either need to remove the newline (add this after your while and before the if):

line[strlen(line) - 1] = '\0';

Or compare it to userword\n.

esorton
  • 1,562
  • 10
  • 14
  • Good catch. @joop is correct. Above code needs some additional error checking to ensure you aren't indexing line[-1]. – esorton Mar 28 '14 at 15:45
  • This is cool. When i add "\n" to the compare string that seems to work. Thanks @esorton – Andy Mar 28 '14 at 17:46
  • Glad you got it working. Both http://stackoverflow.com/questions/13443793/fgets-includes-the-newline-at-the-end and http://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input discuss a variety of ways of dealing with the trailing newline. – esorton Mar 28 '14 at 17:55
0

I'd recommend strncmp() to do the trick since fgets() includes the newline character:

if(!strncmp(line, "userword", strlen(line) - 1) )
jfly
  • 7,715
  • 3
  • 35
  • 65