0

Tried iteration over strlen and random constant variable but unable to solve:

:( spell-checking is case-insensitive
    expected "MISSPELLED WOR...", not "MISSPELLED WOR..."

Here is my full code:

// Implements a dictionary's functionality

#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <strings.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 = 10000;

int word_count = 0;
bool dic_loaded = false;

// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int h = hash(word);
    node *cursor = table[h];
    for (int i = 0; i < 101; i++)
    {
        while (cursor != NULL)
        {
            if (strcasecmp(cursor->word, word) == 0)
            {
                return true;
            }
            else
            {
                cursor = cursor->next;
            }
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int hash = 0;
    int n;
    for (int i = 0; word[i] != '\0'; i++)
    {
        if (isalpha(word[i]))
        {
            n = word[i] - 'a' + 1;
        }
        else
        {
            n = 27;
        }
    }
    return hash = ((hash << 3) + n) % N;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Dictionary not found.\n");
        return false;
    }

    for (int i = 0; i < N; i++)
    {
        table[i] = NULL;
    }

    char word[LENGTH + 1];

    while((fscanf(file, "%s", word)) != EOF)
    {
        word_count++;
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            printf("Malloc error.\n");
            return false;
        }
        strcpy(n->word, word);
        int h = hash(word);
        n->next = table[h];
        table[h] = n;
    }
    dic_loaded = true;
    fclose(file);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    if (dic_loaded == true)
    {
        return word_count;
    }
    else
    {
    return 0;
    }
}

// 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);
        }

    }
    dic_loaded = false;
    return true;
}

I was unable to understand answers on other questions and was helpless. That's why I asked this question seperately.

  • The problem isn't so much related to your check function but more with your hash function (or how you treat what's being passed to your hash function.) As is, your hash function will return a different hash for "Hi" and "hi". – Pelux Jun 4 at 17:42
2

Your word comparison appears to be case insensitive.

What about your hash function? Is it case sensitive? ;-)

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

| improve this answer | |

You must log in to answer this question.

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