1

I finished writing my code for speller. It compiles fine but the only output being printed is "Misspelled words". The words misspelled , words in text, words in dictionary does not get printed. I'm assuming its because the program crashes before then? Here is my code. If only I knew in which function or area my problem lies I might be able to fix it. Also, my hash function is to base hash indexes base on the first two letters of the word.

// Implements a dictionary's functionality

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

#include "dictionary.h"

int dsize = 0;

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// TODO: Choose number of buckets in hash table
const unsigned int N = 676;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // TODO
    // hash word
    int hashnumber = hash(word);
    node *cursor = table[hashnumber];
    // traversing linked list at that hash number
    while(cursor != NULL)
    {
        if((strcasecmp(cursor->word, word) == 0))
        {
            return true;
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int hash;
    // TODO: Improve this hash function
    for( int i = 97; i < 123; i ++)
    {
        hash = (i - 97) * 26;
        // check asciivalue both uppercase and lowercase for first letter
         if(word[0] == (char)i || word[0] == (char)(i - 32) )
         {
             for ( int j = 97; j < 122; j++)
              {
                  // check asciivalue both uppercase and lowercase for second letter
                  if(word[1] == (char)j || word[1] == (char)(j - 32))
                  {
                      hash = hash + (j - 97);
                  }

              }
         }

    }


    return hash;
}

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

     FILE *input = fopen(dictionary, "r");

    if (dictionary== NULL)
    {
        return false;
    }

    node *temp;
    char word[LENGTH + 1];
    while((fscanf(input, "%s", word)) != EOF)
    {

       temp = malloc(sizeof(node));
       if(temp == NULL)
       {
           return false;
       }


       strcpy(temp->word, word);
       int hashnumber = hash(word);
       if (table[hashnumber] == NULL)
       {
           table[hashnumber] = temp;
       }
       else
       {
           temp->next = table[hashnumber];
           table[hashnumber] = temp;
       }
       dsize++;
    }
    fclose(input);

return true;
}

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

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
node *temp;
node *cursor;
    for ( int i = 0; i < N; i++ )
    {
        cursor = table[i];

        while(table[i] != NULL)
        {
            temp = cursor;
            cursor = cursor->next;
            free(temp);
        }
        if(cursor == NULL && i == (N - 1))
        {
            return true;
        }

    }
    return false;
}

  • 1
    This seems like a good time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. Using a *debugger* you can catch crashes as they happen, and locate where in your code they happen, and also examine the values of involved variables at the location and time of the crash. – Some programmer dude Mar 30 '22 at 08:28
  • 1
    One thing to look out for, does the `hash` function return a value between `0` and `N - 1`? – Some programmer dude Mar 30 '22 at 08:29
  • 2
    On another note, your code is full of [*magic numbers*](https://en.wikipedia.org/wiki/Magic_number_(programming)). That's a bad habit. If by e,g, `97` and `123` you mean the ASCII encoded letters `'a'` and `'{'`, the use the actual letters instead. And for convert between upper and lower case use `toupper` and `tolower`. Also note that ASCII is not the only possible encoding, there are character encodings with non-consecutive characters. And the hashing doesn't make much sense anyway, as you don't hash based on the whole string, only the first two characters are used. – Some programmer dude Mar 30 '22 at 08:33

0 Answers0