I'm new to CS and could really use some help on the speller pset. I've got a basic outline that doesn't seem to seg fault, but I'm still having issues. It fails check50 in that:
- it doesn't handle most basic words properly
- spell-checking is not case insensitive
- it doesn't handle substrings properly
- and has memory errors
If I run a tester file through it, the counter only shows that there are 2 words in the dictionary, and so it spits out nearly every word in the document as misspelled (leading me to think there is an error in load, but I can't figure out where).
There's also a valgrind error. It displays the following:
HEAP SUMMARY: in use at exit: 1,300 bytes in 14 blocks. total heap use: 15 allocs, 1 frees, 5406 bytes allocated.
Any assistance would be much appreciated; I've been stuck on this for 3 days!
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.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 = 26;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int numloc = hash(word);
node *cursor = table[numloc];
while (cursor != NULL)
{
if (strcasecmp(cursor->word, word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
unsigned long hash = 5381;
int c;
while ((c = *word++))
{
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash % N;
}
// Loads dictionary into memory, returning true if successful else false
int counter = 0;
bool load(const char *dictionary)
{
FILE *dictionary = fopen(dictionary, "r");
if (dictionary == NULL)
{
return false;
}
char tempword[LENGTH + 1];
for (int i = 0; i < N; i++)
{
table[i] = NULL;
}
while (fscanf(dict, "%s", tempword) != EOF)
{
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
strcpy(n->word, tempword);
int A = hash(tempword);
if (table[A] == NULL)
{
table[A] = n;
n->next = NULL;
}
else
{
n->next = table[A];
table[A] = n;
}
counter++;
}
fclose(dictionary);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
if (counter == 0)
{
unload();
return 1;
}
else
{
return counter;
}
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *cursor = table[i];
node *tmp = table[i];
while (cursor != NULL)
{
cursor = cursor->next;
free(tmp);
tmp = cursor;
}
return true;
}
return false;
}
tolower()ortoupper()will help. - Michael Dorganhash * 33instead of shift/adding. The compiler is plenty smart to use a shift/add if it is more efficient. - Michael Dorgancalloc()malloc()realloc()there must be a call tofree()before the program exits, using the same pointer values as returned by the heap allocation functions. - user3629249gcc, at a minimum use:-Wall -Wextra -Wconversion -pedantic -std=gnu11) Note: other compilers use different options to produce the same results. - user3629249