0

I've spent pretty much time trying to solve a speller and got really stuck, don't know what's wrong with my code. It compiles but check50 says it's totally incorrect. I would be grateful if you could tell me what I am doing wrong.

// Implements a dictionary's functionality

#include <stdbool.h>
#include <strings.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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 = 100;

// Hash table

node *table[N];

// number of words in a dictionary

int wcount = 0;

// Returns true if word is in dictionary else false

bool check(const char *word)
{
    int index = hash(word);

    for (node *cursor = table[index]; cursor != NULL; cursor = cursor->next)
    {
        if (strcasecmp(cursor->word, word) == 0)
        {
            return true;
        }
    }
    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) + tolower(c);
    }
    return hash % N;
}

// Loads dictionary into memory, returning true if successful else false

bool load(const char *dictionary)
{
    FILE *dictptr = fopen(dictionary, "r");
    if (dictptr == NULL)
    {
        return false;
    }

    char tempword[LENGTH + 1];

    while (fscanf(dictptr, "%s", tempword) != EOF)
    {
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }

        strcpy(n->word, tempword);
        n->next = NULL;
        wcount++;

        int index = hash(n->word);

        if (table[index] == NULL)
        {
            table[index] = n;
        }
        else
        {
            n->next = table[index];
            table[index] = n;
        }
    }
    fclose(dictptr);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded

unsigned int size(void)
{
    return wcount;
}

// Unloads dictionary from memory, returning true if successful else false

bool unload(void)
{
    for (int i = 0; i < N; i++)
    {
        node *cursor, *tmp = table[i];

        while (tmp != NULL)
        {
            cursor = cursor->next;
            free(tmp);
            tmp = cursor;
        }
    }
    return true;
}
1

Although you're doing a case-insensitive comparison between the word to be checked and the dictionary words, the code is hashing the word with capital letters. That means that any word with a capital letter will get a different hash than the same word with all lower case letters.

There may be other issues.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • actually the issue was in the unload function, which was buggy and couldn't unload the file. Nevertheless thanks for your quick response, it really helped me to find a solution! – HackeR73 Jun 18 '20 at 11:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .