I have tried to implement load/check function as best as I understand it. When I run it, it prints out all the words in the text as misspelled. I don't even know what is wrong with my code at all. Please can you help me figure out what is wrong with my code.`
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "dictionary.h"
#define ALPHABETS 27
typedef struct node
{
bool is_word;
struct node* children[ALPHABETS];
}node;
node* root;
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
// TODO
int w_index;
int w_len = strlen(word);
for(int i = 0; i < w_len;i++)
{
if(isupper(word[i]))
w_index = (tolower(word[i])) - 'a';
else
w_index = word[i] - 'a';
if(!root->children[w_index])
{
root->is_word = false;
return false;
}
else
root = root->children[w_index];
}
if(root->is_word == true)
return true;
else
return false;
return true;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
// TODO
char word[LENGTH +1];
node* current = NULL;
current = (node*)malloc(sizeof(node));
if(current)
{
int i;
current->is_word = false;
for(i = 0; i < ALPHABETS; i++)
current->children[i] = NULL;
}
root = current;
//char c = 'a';
FILE* file = fopen(dictionary, "r");
if (file == NULL)
{
printf("Could not open %s.\n", dictionary);
unload();
return 1;
}
int index;
while(fscanf(file, "%s", word)!= EOF)
{
int length = strlen(word);
for(int j = 0; j < length; j++)
{
index = word[j] - 'a';
if(!root->children[index])
root->children[index] = malloc(sizeof(node));
root = root->children[index];
}
root->is_word = true;
}
fclose(file);
return true;
}