0

Hi everyone! I am currently trying to finish pset5, but I keep getting seg faults with speller. I am a begginer in programming, so I hope that I will be able to finish it soon! Thanks for your time and any help would be much appreciated!

This is my code:

// Implements a dictionary's functionality

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

// Counter to keep track of the words
int counter = 0;
// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // Convert string to lowercase
    char lower[LENGTH + 1];

    // Iterate through characters and convert each one
    for (int i = 0; i < LENGTH; i++)
    {
        lower[i] = tolower(word[i]);
    }

    // Set cursor
    node *cursor = table[hash(lower)];

    // Traverse list
    while (cursor != NULL)
    {
        if (strcmp(word, cursor->word) == 0)
        {
            return true;
        }
        cursor = cursor->next;
    }
    return false;
}

// Hashes word to a number
// Credit to https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/cf9nlkn/ !!!
unsigned int hash(const char *word)
{
    unsigned int hash = 0;

    for (int i = 0, l = strlen(word); i < l; i++)
    {
        hash = (hash << 2) ^ word[i];
    }

    return hash % N;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Initialize word
    char word[LENGTH + 1];

    // Open dictionary file
    FILE *ptr = fopen(dictionary, "r");

    // Check if the return value is NULL
    if (ptr == NULL)
    {
        return false;
    }

    // Read strings from file
    while (fscanf(ptr, "%s", word) != EOF)
    {
        // Create a new node to store that word into the hash table
        node *node = malloc(sizeof(node));

        // Check if the return value is NULL
        if (node == NULL)
        {
            return false;
        }
        else
        {
            // Copy word into node
            strcpy(node->word, word);

            // Hash word
            int n = hash(node->word);

            // Insert node into hash table
            node->next = table[n];
            table[n] = node;

            // Add to counter
            counter++;
        }
    }
        // Close file
        fclose(ptr);

        return true;
}

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

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

        while(cursor != NULL)
        {
            node *tmp = cursor;
            cursor = cursor->next;
            free(tmp);
        }
        free(cursor);
    }
    return true;
}


You must log in to answer this question.

Browse other questions tagged .