0

Please take a look at my code and help me understand where the problem lies. I have used printf to check that the dictionary is loaded correctly. Also the word that is being checked is same as the word in the text file. I think the problem lies somewhere in the check function but I am not sure.

// Implements a dictionary's functionality

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "dictionary.h"
#include <string.h>
// Represents number of buckets in a hash table
#define N 26

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

int h;
struct node *nptr;
struct node *ptr;

// Represents a hash table
node *hashtable[N];

// Hashes word to a number between 0 and 25, inclusive, based on its first letter
unsigned int hash(const char *word)
{
    return tolower(word[0]) - 'a';
}

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

    // Open dictionary
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        unload();
        return false;
    }

    // Buffer for a word
    char word[LENGTH + 1];

    // Insert words into hash table
    while (fscanf(file, "%s", word) != EOF)
    {
       h =  hash(word);
       ptr = hashtable[h];
       nptr = malloc(sizeof(node));
       strcpy(nptr -> word, word);
       nptr -> next = NULL;
       if (ptr == NULL)
       {
           hashtable[h] = nptr;
       }
       else
       {
           while (ptr -> next != NULL)
           {
               ptr = ptr -> next;
           }
            ptr -> next = nptr;
       }
    }

    // Close dictionary
    fclose(file);

    // Indicate success
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    unsigned int c = 0;
    for (int i = 0; i < N; i++)
    {
        ptr = hashtable[i];
        while (ptr!= NULL)
        {
            ptr = ptr -> next;
            c++;
        }
    }
    return c;
}

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    char wordt [LENGTH + 1];
    for(int i = 0; i < (LENGTH + 1); i++)
    {
        if (word[i] == '\'')
        {
            wordt[i] = word[i];
        }
        else
        {
            wordt[i] = tolower(word[i]);
        }
    }
    h = hash(wordt);
    ptr = hashtable[h];
    while (ptr != NULL)
    {
        if (ptr -> word == wordt)
        return true;
        ptr = ptr -> next;
    }
    return false;
}

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

1 Answer 1

1

Here's the main issue that's blocking your progress:

    if (ptr -> word == wordt)

Your goal is obviously to see if the words are the same. Unfortunately, this will compare addresses in memory, not strings. You need to use strcmp() or strcasecmp(). The latter might make it possible to eliminate some other code, but that's up to you. ;-)

[EDIT] I had some other issues with my IDE that was giving some false bad results while I was testing your code. They have since disappeared.

Now, all you need to do is pass valgrind! ;-)

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 .