i genuinely have no idea where the problem lies so any help would be appreciated, thanks in advance.
check function
bool check(const char *word)
{
trav = root;
int index = 0;
for (int n = strlen(word); index < n; index++)
{
if (trav->children[index] == NULL)
{
return false;
}
else
{
trav = trav->children[index];
}
}
if (trav->is_word == true)
{
return true;
}
else
{
return false;
}
}
load function
bool load(const char *dictionary)
{
// allocate memory for root
root = calloc(1, sizeof(node));
//create cursor
trav = root;
// open dictionary for reading
FILE *inptr = fopen(dictionary, "r");
if (inptr == NULL)
{
printf("Couldn't open the file.\n");
return false;
}
char word[LENGTH + 1];
// scan through the dictionary one word at a time
// and iterate through the trie
while (fscanf(inptr, "%s", word) != EOF)
{
for (int i = 0, n = strlen(word); i < n; i++)
{
// create index for letters in the trie
if (isalpha(word[i]))
{
index = tolower(word[i]) - 'a';
}
else
{
// for apostrophe
index = 26;
}
// check next node
// if null allocate memory and go there
if (trav->children[index] == NULL)
{
trav->children[index] = calloc(1, sizeof(node));
trav = trav->children[index];
}
// if not just go there
else
{
trav = trav->children[index];
}
}
trav->is_word = true;
numwords++;
// set cursor back to root
trav = root;
}
fclose(inptr);
return true;
}
size function
unsigned int size(void)
{
if (numwords != 0)
{
return numwords;
}
else
{
return 0;
}
}
unload function
void freenode (node *cursor)
{
for (int i = 0; i < 27; i++)
{
if (cursor->children[i] != NULL)
{
freenode(cursor->children[i]);
}
else
{
free (cursor);
}
}
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
freenode(root);
return true;
}
check50: https://cs50.me/checks/94a2114125b9303c5b78862123f70745023a2dc6