0

I tried to convert each word to lower before hashing in check, but it seems like it didnt do anything :( however, in the output the misspelled word is now lower

// Implements a dictionary's functionality

#include <stdbool.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 = 2500;

// Hash table
node *table[N];

int s = 0; //size of dictionary

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // TODO
    //int i = hash(word);
    char w[strlen(word)];
    for(int x = 0; x<=strlen(word); x++){
        w[x] = tolower(word[x]);
    }
    int i = hash(w);
    node *cursor = table[i];
    //printf("hash at a is: %i, hash at A is %i\n", hash("a"), hash("A"));
    while(cursor != NULL)
    {
        //printf("word checked is %s\n", cursor->word);
        if (strcasecmp(cursor->word, word) == 0)
        {
            free(cursor);
            return true;
        }
        cursor = cursor->next;
    }
    //free(cursor);
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{

    //hash function retrieved from http://www.cse.yorku.ca/~oz/hash.html
    unsigned int hash = 5381;
    int c;

    while ((c = *word++))
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
    //printf("%i\n",hash%N);
    return (hash % N);
    //return 0;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // TODO
    for ( int x = 0; x < N; x++){
        table[x] = NULL;
    }

    FILE *dict = fopen(dictionary, "r"); //open dictionary file to dict ptr
    if (!dict) //check if dict file opened
    {
        return false;
        fclose(dict);
    }

    char tmp[LENGTH + 1];

    //while (fgets(tmp, LENGTH, dict))
    while (fscanf(dict, "%s", tmp) != EOF)
    {
        //printf("temp is %s\n", tmp);
        int hsh = hash(tmp); //hash the word
        //printf("hash is %i\n", hsh);
        node *n = malloc(sizeof(node)); //allocate node for word
        if (n == NULL)
        {
            free(n);
            fclose(dict);
            return false;
        }
        strcpy(n->word, tmp); //put word in new node
        //printf("n.word is %s\n", n->word);

        //n->next = NULL;

        if(table[hsh] == NULL){
            table[hsh] = n;
            n->next = NULL;
        }
        else{
            n->next = table[hsh];
            table[hsh] = n;
        }
        //printf("hash at %i is %s\n", hsh,table[hsh]->word);
        s++;
        //free(n);
        //fclose(dict);
        //return true;

    }
    //free(n);
    fclose(dict);
    return true;
}

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

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

I don't know what sort of output you're getting, but at first glance freeing a node whenever it's found in the dictionary will definitely cause you problems.

3
  • 1
    Definitely looks like your main problem is the call to free(cursor). This means that as soon as you find a word that's spelled correctly, you remove it from the table and orphan the linked list. If it appears in the text again it will now be considered misspelled. – Sentox Dec 20 '20 at 5:45
  • that was the problem, fixed – Michael Weiser Dec 20 '20 at 6:28

You must log in to answer this question.

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