My program is misspelling all words in the texts. There is a problem somewhere in either load or check, but I just can't figure it out. Can you guys help me find the problems I am having in my program? Thanks! My codes are posted below:
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"
// Represents number of buckets in a hash table
#define N 26
// Represents a node in a hash table
typedef struct node
{
// LENGTH + 1 is because an array always need an extra space at the end for /0
char word[LENGTH + 1];
struct node *next;
}
node;
// Represents a hash table
node *hashtable[N];
int wordcount = 0;
// Hashes word to a number between 0 and 25, inclusive, based on its first letter - Determing where the word should be added to in hastable
unsigned int hash(const char *word)
{
// tolower turns word[0], first letter of the word, into a lower case letter
// take the value of word[0], subtract it by the value of 'a' in ASCII and return the bucket number for that letter in hashtable
return tolower(word[0]) - 'a';
}
// Loads dictionary into memory, returning true if successful else false. Adding words from dictionary file to hashtable
// word refers to words in the dictionary file
bool load(const char *dictionary)
{
// Initialize hash table, turning all its bucket values to NULL
for (int i = 0; i < N; i++)
{
hashtable[i] = NULL;
}
// Open dictionary
FILE *file = fopen(dictionary, "r");
// if file is invalid, return false, and unload it from memory
if (file == NULL)
{
unload();
return false;
}
// Buffer for a word, create space to store the word
char word[LENGTH + 1];
// Insert words into hash table:
// run through each word in the dictionary
while (fscanf(file, "%s", word) != EOF)
{
// create space for a new node
node *new_node = malloc(sizeof(node));
// if no more space remains in memory, unload and return false
if (new_node == NULL)
{
unload();
return false;
}
// add the value, word, into the newly created node
strcpy(new_node->word, word);
// get the index of the bucket using the hash function
int i = hash(word);
// assign the pointer next of the bucket node as head
node *head = hashtable[i];
// flag == false means this is the first node
if (head == NULL)
{
// point head to the newly created node
head = new_node;
// point the pointer next of new node to NULL
new_node->next = NULL;
}
// flag == true means this is NOT the first node
else
{
// point the pointer next of new node to where head is pointing to
new_node->next = head;
// point the head to the new node
head = new_node;
}
wordcount++;
}
// 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)
{
return wordcount;
}
// Returns true if word is in dictionary else false. Check if the words are in the hashtable/dictionary
// word refers to the words in the text file
bool check(const char *word)
{
int i = hash(word);
node *cursor = hashtable[i];
while (cursor != NULL)
{
if (strcasecmp(cursor->word, word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
return false;
}