I have posted this questions a few days ago, but none of us were getting anywhere. I thought maybe including my code for load
as well as check
might yield some new revelations. Anyway, here is the issue:
Check50 is saying my code fails on possessives and substrings, but I'm not quite sure why. First of all, here is the check50 output.
Next, here is my code for load
:
bool load(const char* dictionary)
{
//load dictionary and check if it failed
FILE* dictptr = fopen(dictionary, "r");
if(dictptr == NULL)
{
return false;
}
//node initializations
root = calloc(1, sizeof(node));
//load first character from dictionary
int letter = fgetc(dictptr);
//position integer for later
int pos;
prevnode = root;
//start loading loop
while(letter != EOF)
{
//if at the end of a word
if(letter == '\n' && prevnode != NULL)
{
prevnode->is_word = true;
prevnode = root;
wordcount++;
}
else if(letter != '\n')
{
//if letter is apostrophe
if(letter == '\'')
{
//if need new node
if(prevnode->children[26] == NULL)
{
prevnode->children[26] = calloc(1, sizeof(node));
}
//if don't need new node
prevnode = prevnode->children[26];
}
//if letter is a letter
else if(isalpha(letter))
{
//determine indexing position
pos = toupper(letter) - 'A';
//if need new node
if(prevnode->children[pos] == NULL)
{
prevnode->children[pos] = calloc(1, sizeof(node));
}
//if don't need new node
prevnode = prevnode->children[pos];
}
}
letter = fgetc(dictptr);
}
fclose(dictptr);
return true;
}
And here is my code for check
:
bool check(const char* word)
{
//position integer for later
int wpos;
//present letter in the tested word
int wletter;
//reset prevnode
prevnode = root;
//iterate over word
for(int i = 0, n = strlen(word); i < n; i++)
{
//load in [i]th letter of word to be checked
wletter = word[i];
//if checking letter
if(isalpha(wletter))
{
//ignore case
wpos = toupper(wletter) - 'A';
//if misspelled
if(prevnode->children[wpos] == NULL)
{
return false;
break;
}
//letter present in trie, move on
else if(prevnode->children[wpos] != NULL)
{
prevnode = prevnode->children[wpos];
}
}
//if checking apostrophe
else if(wletter == '\'')
{
//if misspelled
if(prevnode->children[26] == NULL)
{
return false;
break;
}
//apostrophe present in trie, move on
else if(prevnode->children[26] != NULL)
{
prevnode = prevnode->children[26];
}
}
//if end of checked word
else if(wletter == '\0')
{
//correctly spelled
if(prevnode->is_word == true)
{
return true;
break;
}
//incorrectly spelled
else if(prevnode->is_word != true)
{
return false;
break;
}
}
}
return true;
}
Variables like root
and prevnode
are declared at the top of dictionary.c
.
Any insight is GREATLY appreciated. This "seems" to be my last issue. Once this is solved, I can finally put this pset to bed.
Thanks in advance!