I need help, I can't get around this problem set. I have searched everywhere and I think the code is equal to most of the other students... The problem is that the check function is returning true every time (I think?) and as a result all the words result as misspelled and I can't see where I'm doing something wrong.
Interesting fact: If I swap true and false i get some of check50 goals correct but it is wrong as I need to return true when the word is the dictionary.
// Implements a dictionary's functionality
#include <stdio.h>
#include <stdbool.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 = 27;
// Hash table
node *table[N] = {NULL};
// Dictionary word count
int word_count = 0;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
node *cursor = table[hash(word)];
while (cursor != NULL)
{
if (strcasecmp(word, cursor->word) == 0)
{
return true;
}
else
{
cursor = cursor->next;
}
}
return false;
}
// Hashes word ()
unsigned int hash(const char *word)
{
unsigned int value = word[0];
if (value >= 'a' && value <= 'z')
{
value = value - 97;
}
else if (value >= 'A' && value <= 'Z')
{
value = value - 65;
}
return (value % N);
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
return false;
}
char dic_word[LENGTH + 1];
while (fscanf(file, "%s", dic_word) != EOF)
{
node *new_n = malloc(sizeof(node));
if (new_n == NULL)
{
unload();
fclose(file);
return false;
}
strcpy(new_n->word, dic_word);
new_n->next = NULL;
int hash_index = hash(dic_word);
node *head = table[hash_index];
if (head == NULL)
{
head = new_n;
}
else
new_n->next = head;
head = new_n;
}
word_count++;
free(new_n);
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return word_count;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *cursor = table[i];
while (cursor != NULL)
{
node *temp = cursor;
cursor = cursor->next;
free(temp);
}
free(cursor);
}
return true;
}
RESULTS:
:) dictionary.c, dictionary.h, and Makefile exist
:) speller compiles
:( handles most basic words properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( handles min length (1-char) words expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( handles max length (45-char) words expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( handles words with apostrophes properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( spell-checking is case-insensitive expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( handles substrings properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:| program is free of memory errors can't check until a frown turns upside down
RESULTS (True and False swapped):
:) dictionary.c, dictionary.h, and Makefile exist
:) speller compiles
:) handles most basic words properly
:) handles min length (1-char) words
:) handles max length (45-char) words
:( handles words with apostrophes properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..." :) spell-checking is case-insensitive
:( handles substrings properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:| program is free of memory errors can't check until a frown turns upside down