Running the program, a large portion of text is incorrectly flagged as mis-spelled. I've reviewed & changed the hash / check / load functions but I end up with the same issue. Can someone point me to the issues in my code?
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Number of buckets in hash table
const unsigned int N = 676;
// Hash table
node *table[N];
int wordcounter = 0;
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
// TODO
node *list = table[hash(word)];
if (list == NULL)
{
return false;
}
while(list!=NULL)
{
if(strcasecmp(word, list->word) == 0)
{
return true;
}
list = list -> next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
// TODO
char copy_word[LENGTH + 1];
strcpy(copy_word, word);
for(int i = 0; i < strlen(word); i++)
{
copy_word[i] = tolower(copy_word[i]);
}
unsigned int hash = 5381;
int c;
for(int i = 0; i < strlen(copy_word); i++)
{
c = copy_word[i];
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash % N;
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// TODO
FILE *f = fopen(dictionary, "r");
char temp_word[LENGTH +1];
//checking if file can be opened
if (f == NULL)
{
printf("Could not open dictionary.\n");
return false;
}
while(fscanf(f, "%s", temp_word)!= EOF)
{
node *w = malloc(sizeof(w));
if ( w == NULL)
{
return 1;
}
strcpy(w -> word, temp_word);
w -> next = NULL;
wordcounter++;
if (table[hash(temp_word)] == NULL)
{
table[hash(temp_word)] = w;
}
else if(table[hash(temp_word)] != NULL)
{
w -> next = table[hash(temp_word)];
table[hash(temp_word)] = w;
}
}
//closing file
fclose(f);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
// TODO
if (wordcounter!=0)
{
return wordcounter;
}
else
{
return 0;
}
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
// TODO
node *temp;
node *list;
for(int n = 0; n < N; n++)
{
list = table[n];
while(list!=NULL)
{
temp = list -> next;
free(list);
list = temp;
}
}
return true;
}