I am still trying to finish the speller pset but without success. I can now get it to compile but its output is incorrect. When I create a smaller dictionary and test text file, with 12 words identical and 1 spelt differently (i.e. so 13 words in total) I get words mispelled:10, words in dictionary:13 and words in text: 12. I have run GDB etc but just cant figure it out. I have spent more time on this pset that the rest combined! Its only stubborness that is keeping me going at this stage. I have set out below the code for my check and load functions. If anyone could give me some guidance on where I need to be looking it would be greatly appreciated, I am stumped!
bool load(const char* dictionary)
{
//initialise hashtable to NULL
for (int i = 0; i < 26; i++)
{
hashtable[i] = NULL;
}
// open dictionary file and read it
FILE* dic = fopen(dictionary, "r");
//if empty return false
if (dic == NULL)
{
printf("Failed to load dictionary.\n");
return false;
}
else
{
//create temp storage for words
char buffer[LENGTH + 1];
//continue until end of file reached
while (fscanf(dic, "%s", buffer) != EOF)
{
//build a new node
node* new_node = malloc(sizeof(node));
//copy the word from buffer into the new node
strcpy(new_node->word, buffer);
count++;
//put word through hash function to get index for the array
index = hashy(new_node-> word);
//if first word at index, put it in and point to Null
if (hashtable[index] == NULL)
{
new_node->next = NULL;
hashtable[index] = new_node;
}
//if there is already a word at the index, add the new word
else
{
new_node->next = hashtable[index];
hashtable[index] = new_node;
}
}
}
fclose(dic);
return true;
}
And my check function
bool check(const char* word)
{
//get length of word and store in p
int p = strlen(word);
//set aside memory for array containting word
char worda[LENGTH + 1];
//ensure all lower case letters
for (int i = 0; i < p; i++)
{
worda[i] = tolower(word[i]);
}
int q = hashy(worda);
if (hashtable[q] == NULL)
{
return false;
}
node* cursor = hashtable[q];
while(cursor != NULL)
{
if(strcmp(cursor->word,worda) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}