0

i'm having some trouble with speller, my code seems to detect words in dictionary and words in text fine, based off of check50 results, but for each check it seems all of my words come back as misspelled, would really appreciate some help as i'm really struggling with this. I suspect it has something to do with my check function. Thanks, here is my code.

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



int word_count;

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

// Hash table
node *table[N];

//create hash table
//reference https://youtu.be/2Ti5yvumFTU
void init_table()
{
    for (int i = 0; i < N; i++)
    {
        table[i] = NULL;
    }
}



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

// Hashes word to a number
// hash function: djb2
// from http://www.cse.yorku.ca/~oz/hash.html
unsigned int hash (const char *word)

{

unsigned long hash = N;

int c = *word;

c = tolower(c);

while (*word != 0)

{

hash = ((hash << 5) + hash) + c;

c = *word++;

c = tolower(c);

}

return hash % N;

}

// Loads dictionary into memory, returning true if successful else false
// lots of help from stackoverflow used for this function
bool load(const char *dictionary)
{


    FILE *dictfile = fopen(dictionary, "r");  //open the dictionary

    if (dictfile == NULL) //if file fails to open return false

        {
            printf("FILE COULD NOT BE LOADED");

            return false;
        }

    init_table(); //initializes hash table

    node *head; //creates head node


    char dictword[LENGTH - 1]; //makes array for word (length - 1 cause 0 index)


    while (fscanf(dictfile, "%s", dictword) != EOF) //iterate until end of the file
    {
        node *n = malloc(sizeof(node)); //create a new node each iteration for each new word
        if (n == NULL)
        {
            return false; //if mallocd memory is null then return false
        }

        int index = hash(n->word); //hash the word part of the node

        strcpy (n->word, dictword); //copies the word scanned by fscanf and puts it into n->word part of the node

        if (table[index] == NULL) //if space in table hasnt been used yet set index[n] to head
        {
            head = table[index] = n;
            n->next = NULL;
            word_count++;
        }

        else //if collision occurs, add to the chain
        {
            n->next = head;
            table[index] = NULL;
            word_count++;
        }

    }


    return true;
}

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

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    
    for (int i = 0; i < 25; i++)
    {
    node *cursor = table[i];
        while (cursor != NULL)
        {
            node *tmp = cursor;
            cursor = cursor->next;
            free(tmp);
            //printf("boobs\n");
        }
        
    }
    
    return true;
}
  • I suspect this line in your load function. int index = hash(n->word); //hash the word part of the node since n is still a pointer to empty node (after the malloc i think you should put it some value) – sulai_x Jun 26 at 9:12
1

I've found at least 3 issues with the code.

First, in load(), the code is trying to hash n->word before the word is copied into n->word.

Second, introducing head doesn't really help. Try eliminating head from the code.

Finally, in check() the code needs to hash each word after converting to lower case. If it hashes the same word with upper case letters, it will produce a different hash.

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 .