0

new error: most words are seen as spelling error

Here's my full code: (UPDATED)

// Implements a dictionary's functionality

#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>

#include "dictionary.h"
#define largenum 10000

// 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 = 200000;

// Hash table
node *table[N];

//counting number of words in the dictionary
int word_count = 0;

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    int length = strlen(word);
    char lowercase[length];
    for (int i = 0; lowercase[i] != '\0'; i ++)
    {
        lowercase[i] = tolower(word[i]);
    }
    
    unsigned int index = hash(lowercase);
    node *cursor = table[index];
    
    while (cursor != NULL)
    {
        if (strcasecmp(lowercase, cursor->word) == 0)
        {
            return true;
        }
        else
        {
            cursor = cursor->next;
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // SDBM Hash
    unsigned int hash = 0;
    for (int i = 0, n = strlen(word); i < n; i ++)
    {
        hash = (word[i]) + (hash << 6) + (hash << 16) - hash;
    }
    return hash % largenum;
}

// Loads dictionary into memory, returning true if successful, else false
// load into hash
bool load(const char *dictionary)
{
   // opening dictionary
    FILE *dict = fopen(dictionary, "r");
    if(dict == NULL)
    {
        return false;
    }
    char word[LENGTH + 1];
    
    for (int i = 0; i < N; i ++)
    {
        table[i] = NULL;
    }
    // read string from file and create a new node for each word
    while(fscanf(dict, "%s", word) != EOF)
    {
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            unload();
            return false;
        }
        
        strcpy(n->word, word);
        int index = hash(n->word);
        
        if (table[index] == NULL)
        {
            table[index] = malloc(sizeof(node));
            strcpy(table[index]->word, word);
            table[index]->next = NULL;
        }
        else
        {
            n->next = table[index];
            table[index] = n;
        }
        
        word_count ++;
    
    }
    fclose(dict);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // need to iterate over entire hash table
    for (int i = 0; i < N; i ++)
    {
        node *cursor = table[i];
        node *tmp = table[i];

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

0

Best guess: the hash function returns a value bigger than N (the defined table size). Perhaps add a debugging printf to verify. This can corrected using the modulo operator %.

4
  • New error: most words are misspelled (I updated my code). – Cathy Choo Jun 3 at 19:26
  • Technically, new question. Maybe because lowercase is not null terminated. – DinoCoderSaurus Jun 3 at 21:01
  • I updated my code but I'm still getting words that are supposed to be correct as misspelled. – Cathy Choo Jun 3 at 21:08
  • lowercase still not null terminated. – DinoCoderSaurus Jun 4 at 0:27

You must log in to answer this question.

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