Skip to main content
2 of 2
added 134 characters in body
Pexan
  • 19
  • 6

Pset4: dictionaries with hashtable -- every word is misspelled

I'm getting every word is misspelled when I run

./speller dictionaries/large texts/pneumonoultramicroscopicsilicovolcanoconiosis.txt

When I run help50:

Sorry, help50 does not yet know how to help with this!

This is my code:

// Implements a dictionary's functionality

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.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
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// 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)
    {
        if (hashtable[hash(word)] == NULL)
        {

            node *word_n = malloc(sizeof(node));
            strcpy(word_n->word, word);
            hashtable[hash(word)] = word_n;
        }
        else
        {
            node *word_n = malloc(sizeof(node));
            strcpy(word_n->word, word);
            node *pointer = hashtable[hash(word)];
            hashtable[hash(word)] = word_n;
            word_n = pointer;
        }
    }

    // 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)
{
    int counter = 0;
    for (int i = 0; i < N; i++)
    {
        node *trv = hashtable[i];
        while(trv != NULL)
        {
            counter++;
            trv = trv->next;
        }
    }
    return counter;
}

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    node *trv = hashtable[hash(word)];
    while(trv != NULL)
    {
        if (strcasecmp(word,trv->word) == 0)
        {
            return true;
        }
        else
        {
            trv = trv->next;
        }
    }
    return false;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for (int i = 0; i < N; i++) {
        node* hash_trv = hashtable[i];
        while (hash_trv != NULL) {
            node* temp = hash_trv;
            hash_trv = hash_trv->next;
            free(temp);
        }
    }
    return true;
}

EDIT: So I've narrowed it down to: in my check() function word and trv->word are never the same. My 'trv->word' is always garbage.

Pexan
  • 19
  • 6