0

using some printf statements to debug it seems that when I hash a word in my load function I get a different value then when I hash it in my check function, even when the words are the same case. I'm trying to understand why this happens but I have no clue.

// 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);
    node *cursor = table[i];
    //printf("check");
    //printf("test: %s\n", table[i]->word);
    printf("hash at %i is %s\n", i,table[i]->word);
    while(cursor != NULL)
    {
        //printf("word checked is %s\n", cursor->word);
        if (strcmp(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;
    }

    char tmp[LENGTH + 1];

    while (fgets(tmp, LENGTH, dict))
    {
        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)
        {
            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);
        //return true;

    }
    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)
{
    // TODO
    return false;
}
1

At least one problem is fgets. From the doc

If a newline is read, it is stored into the buffer. A terminating null byte (aq\0aq) is stored after the last character in the buffer.

All words from dictionary will be stored with \n.

You must log in to answer this question.

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