Is there anyone that can help with my check function on pset5. I've got load working fine and confirmed that the correct number of words are loading in both the small and large dictionaries. I've set a copy of the word so that I can change the letter to lowercase if it's upper case. The a-1 is so that an apostrophe (ascii code 96 and one before a) is assigned to index 0 and then the others are assigned up 1 through 27.
I'm not quite sure where the seg fault would occur. I'm trying to loop through each letter of the word, and following the pointers through the trie and then once the loop is complete I'm asking to check whether the pointer is pointing to a NULL and if not then move on. At the end of the word, check whether is_word is true or not.
Is the pointer going one level too far or something? I can't quite find the seg fault. Thanks in advance for any help.
bool check(const char* word)
{
//set trav to root
node* trav = root;
char word2[strlen(word)];
strcpy(word2, word);
//loop through each letter of the word
for (int i = 0 ; i < strlen(word2) ; i++)
{
//set index using to lower. Use a-1 to set ' to 0 and other letters 1-27
int index = tolower(word2[i])-('a'-1);
//if trav-> children is NULL then return false
if (trav->children[index] == NULL)
{
return false;
}
//else pointer must exist so move trav on
else {
trav = trav->children[index];
}
}
//exists for loop at the end of the word. Check if it's a word.
if (trav->is_word == true)
{
return true;
}
else {
return false;
}
return false;
}