0

Basically when I run the test with texts/lalaland.txt and compare to the staff's key, they have something like 955 misspelled words and I have all but 5 misspelled. I tried to but a "tolower" in my hash code, and still didn't work

// Implements a dictionary's functionality

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

// Hash table
node *table[N];

int word_number = 0;

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

// Hashes word to a number
// hash function by yangrq1018 on GitHub (Rocky Yang)
unsigned int hash(const char *word)
{
    unsigned int hash = 0;
    char* lower = NULL;
    
    for (int j = 0; j < strlen(word); j++)
    {
        lower[j] = tolower(word[j]);
    }
    
    for (int i = 0, h = strlen(word); i < h; i++)
    {
        hash = (hash << 2) ^ lower[i];
    }
    return hash % N;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE *text = fopen(dictionary, "r");
    if (ferror(text))
    {
        return false;
    }
    if (text == NULL)
    {
        return false;
    }
    
    char word_list[LENGTH + 1];
    while (fscanf(text, "%s", word_list) != EOF)
    {
        word_number++;
        //printf("%i\n", word_number);
        
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }

        strcpy(n->word, word_list);
        
        unsigned int bucket = hash(dictionary);
        if (table[bucket] == NULL)
        {
            table[bucket] = n;
            n->next = NULL;
        }
        else
        {
            n->next = table[bucket];
            table[bucket] = n;
        }
    }
    fclose(text);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    if (word_number != -1)
    {
        printf("World Count: %i\n", word_number);
        return word_number;
    }
    else
    {
        return 0;
    }
}

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

When you added the code for tolower() into the hash function, it introduced a seg fault. The tolower code is needed, but you need to fix it. Hint: where is space allocated for lower[]?

Next question. In load,

    unsigned int bucket = hash(dictionary);

Wouldn't it be more useful to get the hash of the word instead of the filename of the dictionary file??? ;-)

Just out of curiosity, did you run this under debug50, or any debugger?

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

| improve this answer | |
  • for some reason when I malloc'd space for tolower, it then told me my seg fault was on the unsigned int bucket line, and I changed dictionary to n->word, and now the program works, so thanks – Matthew Jul 16 at 2:17

You must log in to answer this question.

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