I am reading from my dictionary and printing out the word + the length of the word for testing purposes.
I use strlen to get the length of the string. However, the numbers I got are not correct. I believe strlen doesn't count the \0 character.
I am reading the first 10 words in the dictionary. My expected output should be:
W:A L:1
W:A's L:3
W:AA's L:4
W:AB's L:4
W:ABM's L:5
W:AC's L:4
W:ACTH's L:6
W:AI's L:3
W:AIDS's L:6
W:AM's L:4
But this is what I got (Notice how the L:'s are on another line. I think this is where the problem is):
W:A
L:2
W:A's
L:4
W:AA's
L:5
W:AB's
L:5
W:ABM's
L:6
W:AC's
L:5
W:ACTH's
L:7
W:AI's
L:5
W:AIDS's
L:7
W:AM's
L:5
Below is my code:
FILE* dict = fopen("/usr/share/dict/words", "r"); //open the dictionary for read-only access
if(dict == NULL) {
return;
}
int i;
i = 0;
// Read each line of the file, and insert the word in hash table
char word[128];
while(i < 10 && fgets(word, sizeof(word), dict) != NULL) {
printf("W:%s L:%d\n", word, (int)strlen(word));
i++;
}