Please take a look at my code and help me understand where the problem lies. I have used printf to check that the dictionary is loaded correctly. Also the word that is being checked is same as the word in the text file. I think the problem lies somewhere in the check function but I am not sure.
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "dictionary.h"
#include <string.h>
// Represents number of buckets in a hash table
#define N 26
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
int h;
struct node *nptr;
struct node *ptr;
// Represents a hash table
node *hashtable[N];
// Hashes word to a number between 0 and 25, inclusive, based on its first letter
unsigned int hash(const char *word)
{
return tolower(word[0]) - 'a';
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Initialize hash table
for (int i = 0; i < N; i++)
{
hashtable[i] = NULL;
}
// Open dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
unload();
return false;
}
// Buffer for a word
char word[LENGTH + 1];
// Insert words into hash table
while (fscanf(file, "%s", word) != EOF)
{
h = hash(word);
ptr = hashtable[h];
nptr = malloc(sizeof(node));
strcpy(nptr -> word, word);
nptr -> next = NULL;
if (ptr == NULL)
{
hashtable[h] = nptr;
}
else
{
while (ptr -> next != NULL)
{
ptr = ptr -> next;
}
ptr -> next = nptr;
}
}
// Close dictionary
fclose(file);
// Indicate success
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
unsigned int c = 0;
for (int i = 0; i < N; i++)
{
ptr = hashtable[i];
while (ptr!= NULL)
{
ptr = ptr -> next;
c++;
}
}
return c;
}
// Returns true if word is in dictionary else false
bool check(const char *word)
{
char wordt [LENGTH + 1];
for(int i = 0; i < (LENGTH + 1); i++)
{
if (word[i] == '\'')
{
wordt[i] = word[i];
}
else
{
wordt[i] = tolower(word[i]);
}
}
h = hash(wordt);
ptr = hashtable[h];
while (ptr != NULL)
{
if (ptr -> word == wordt)
return true;
ptr = ptr -> next;
}
return false;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
ptr = hashtable [i];
while (ptr != NULL)
{
nptr = ptr;
ptr = ptr -> next;
free (nptr);
}
hashtable [i] = NULL;
}
for (int i = 0; i < N; i++)
{
if (hashtable[i])
return false;
}
return true;
}