the task is to create a fully functioning spell check, I think I have more than one error in my code which is causing lots of difficulty when trying to wrap my head round what exactly is causing all the frowns on check50. check50 only says my code compiles, everything else is a frown, I think my programme could be exiting during the check function as size and unload have no runtime. I also get exit code 1 once run, however I may be wayyy off as this is my first programming course. Any tips, help or pointers in the right direction would be massivly appriciated!
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include "dictionary.h"
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
} node;
// TODO: Choose number of buckets in hash table
const unsigned int N = 1000;
// Hash table
node *table[N];
bool check(const char *word)
{
// hash word
int x = hash(word);
// create cursor, set to first item in linked list
node *cursor = table[x];
// loop over hash tables
while (cursor != NULL)
{
if (strcasecmp(word, cursor->word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
int dictionary_size = 0; // global variable for size of dictionary, used in multiple functions
unsigned int hash(const char *word)
{
int value = 0;
// hash function using math of all leters
// loop over every word
for (int i = 0; i < strlen(word); i++)
{
// convert to lower case for ascii values, removes case sensitive problem
value += tolower(word[i]); // sum of ascii values of word
}
return value % N; // return index for word
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// TODO
// array to store words from dictionary
char word[LENGTH + 1];
// open dictionary file
FILE *d = fopen(dictionary, "r");
if (d == NULL)
{
return false;
}
// read strings from file repeat for each word in dictionary , similar loop to recover.c
while (fscanf(d, "%s", word) != EOF)
{
// keep track of dictionary size
dictionary_size++;
// create new node
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
// store word in array
strcpy(n->word, word);
n->next = NULL;
// hash word
int x = hash(word);
// set pointers to correct order
n->next = table[x];
table[x] = n;
}
fclose(d);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
return dictionary_size;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
// loop over hash tables
for (int j = 0; j < N; j++)
{
// initialise cursor for local scope
node *cursor = table[j];
// traverse linked list
while (cursor != NULL)
{
node *tmp = cursor;
cursor = cursor->next;
free(tmp);
return true;
}
}
return false;
}
From comments:
in regards to the frowns the only one i have now is: :( programme is free of memory errors. check50 was saying everything was wrong, could've been an error with it
The unload
function frees one node and then return
s (to the call from speller). The function should not return until it has freed all the nodes (ie finished the while loop).