0

Implements a dictionary's functionality

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

Hash table node *table[N];

Returns true if word is in dictionary else false

bool check(const char *word)
{
    //change letters to lowercase

    int length = strlen(word);

    char copy[length + 1];

    for (int i = 0; i < length; i++)
    {
        copy[i] = tolower(word[i]);
    }

    int copy_index = hash(copy);

    //sed pointer to the head of linked list
    node *head = table[copy_index];

    node *cursor = head;

    if (head != NULL)

    {
          while (cursor != NULL)
       {
            if (strcasecmp(copy,cursor -> word) == 0)
          {
              return true;
          }

          else
          {
             cursor = cursor -> next;
          }

        }
    }

    return false;


}

Hashes word to a number

unsigned int hash(const char *word)
{
    // TODO
    unsigned long hash = 5381;
    int c;

    while ((c = *word++))
    hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    return hash;
}

Loads dictionary into memory, returning true if successful else false

bool load(const char *dictionary)
{

    FILE* source = fopen(dictionary , "r" );
    // check the file is not empty
    if (!source)
    {
        fprintf(stderr, "Couldn't open dictionary.\n");
        return false;
    }

    char WORD[45];
    int count = 0;

    while (fscanf(source, "%s", WORD) != EOF)
    {

        struct node *wnode = malloc(sizeof(node));

        if (wnode == NULL)
        {
            unload();
            return false;
        }

        strcpy(wnode->word, WORD);

        unsigned long key = hash(wnode->word);

       //insert word into hash table. if first word of the linked list:
        if(table[key] == NULL)
        {
            table[key] = wnode;
        }

        else
        {
            wnode->next = table[key];
            table[key] = wnode;
        }

    }


    return true;

}

Returns number of words in dictionary if loaded else 0 if not yet loaded

unsigned int size(void)
{
    // TODO
    return 0;
}

Unloads dictionary from memory, returning true if successful else false

bool unload(void)
{

    for( int i = 0; i < N; i++)
    {
        node *head = table[i];
        node *cursor = head;
        if ( cursor != NULL)
        {
            node *tmp = cursor;
            cursor = cursor -> next;
            free(tmp);
        }
    }
    return false;
}
1
  • Can you please add the code for all declarations, globals, and especially the code that declares, initializes or sets values in table[], and the hash function code? Edit the question and add the code. My bet is that there's a problem with the declaration and initialization of table. – Cliff B May 22 '20 at 6:08
0

In addition, array of size 46 is being given to array of size 45 when longest word is copied.

typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

and

char WORD[45];

This would cause fscanf to access memory illegally or not take null terminating character. use

char WORD[46];

hope this was helpful.

2

The problem is that the hash function can return a hash number that's outside of the size of the table[N] array. Given that N = 1, the array only has 1 element, table[0]. THAT'S why you're getting a seg fault. The code is trying to access an array element that doesn't exist.

Suggest changing the hash function return line as follows:

return hash % N;  //limits hash to 0 to N-1.

Also, using a variable name inside a function of the same name is a bad practice. You might want to change the name of the hash variable inside the hash function.

There might be other problems, but that's a big issue.

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

You must log in to answer this question.

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