Basically when I run the test with texts/lalaland.txt and compare to the staff's key, they have something like 955 misspelled words and I have all but 5 misspelled. I tried to but a "tolower" in my hash code, and still didn't work
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include "dictionary.h"
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Number of buckets in hash table
const unsigned int N = 65366;
// Hash table
node *table[N];
int word_number = 0;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int hashed = hash(word);
node *cursor = table[hashed];
while (cursor != NULL)
{
if (strcasecmp(cursor->word, word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
// hash function by yangrq1018 on GitHub (Rocky Yang)
unsigned int hash(const char *word)
{
unsigned int hash = 0;
char* lower = NULL;
for (int j = 0; j < strlen(word); j++)
{
lower[j] = tolower(word[j]);
}
for (int i = 0, h = strlen(word); i < h; i++)
{
hash = (hash << 2) ^ lower[i];
}
return hash % N;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
FILE *text = fopen(dictionary, "r");
if (ferror(text))
{
return false;
}
if (text == NULL)
{
return false;
}
char word_list[LENGTH + 1];
while (fscanf(text, "%s", word_list) != EOF)
{
word_number++;
//printf("%i\n", word_number);
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
strcpy(n->word, word_list);
unsigned int bucket = hash(dictionary);
if (table[bucket] == NULL)
{
table[bucket] = n;
n->next = NULL;
}
else
{
n->next = table[bucket];
table[bucket] = n;
}
}
fclose(text);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
if (word_number != -1)
{
printf("World Count: %i\n", word_number);
return word_number;
}
else
{
return 0;
}
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
node* tmp = NULL;
node* pointer = NULL;
for (int i = 0; i < N; i++)
{
pointer = table[i];
while (pointer != NULL)
{
tmp = pointer;
pointer = pointer->next;
free(tmp);
}
}
free(pointer);
return true;
}