Running this through debug50
with a 143,091 word dictionary and a 19,190 word text would be....pick a word.....daunting. Try debugging with a one word file used as dictionary and text (something like ./speller word.txt word.txt
).
Put a break point at check
. Add a watch expression for word_copy[i]
and step through.
Or study this code carefully to figure out why word_copy is never populated correctly:
for ( int i = 0; i < length; i++)
{
if (isupper(word_copy[i]))
{
word_copy[i] = tolower(word_copy[i]);
}
else
{
word_copy[i] = word_copy[i];
}
}
But wait there's more!
I cannot explain why the screen shots show the correct number of "words in dictionary". This while (!feof(fp))
will always do an extra read on the file. The best explanation can be found around 7:30 in [this short][1] (from last year). Better to make sure that fscanf(fp, "%s", word_dic) != EOF
. You could incorporate it into your while loop or add another if condition.
Have a closer look at the (corrected) code in check:
int length = strlen(word) + 1;
char word_copy[length];
// convert word to lowercase and store it in word_copy
for ( int i = 0; i < length; i++)
{
word_copy[i] = tolower(word[i]);
}
// add null-terminator to mark end of string since strlen doesn't include the NULL character to its length
word_copy[length] = '\0';
If the word is 'cat', it has length 3, indexes 0-2, with null terminator in index 3. Where is it putting the null terminator here word_copy[length] = '\0'
?
[1]: https://www.youtube.com/watch?v=KwvObCA04dU