0

I successfully managed to make the code compile and removed all segmentation faults, but now it returns the problem as described in the title. I assume the problem is in load, as it appears to not be cycling through the words but getting stuck on the first word somehow, Even then I assume check is not working properly because "a" is a word in the dictionary and should not be returning as misspelled. I included just the relevant functions and libraries at top here. Any advice?

#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>

#include "dictionary.h"
    
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // get hash value of word
    int checkHash = hash(word);
    node *cursor = table[checkHash];


// if next is NULL without finding a matching word, return false
while (cursor->next != NULL)
{
    // compare words using strcasecmp
    if (strcasecmp(cursor->word, word))
    {
        return true;
    }
    // use cursor->next to traverse linked list
    cursor = cursor->next;
}
return false;
}

// Hashes word to a number (sum of ascii values + word length)
unsigned int hash(const char *word)
{
    // initializes sum value
    int sum = 0;
    int s;
    // loop that adds ascii value of each letter (zero indexed) to current sum
    for (int i = 0; i < strlen(word); i++)
    {
        s = toupper(word[i]);
        if (isalpha(s))
        {
            int Ascii = s - 65;
            sum += Ascii;
        }
    }
    // adds length of word to sum of ascii values
    sum += strlen(word);
    return sum;
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // opens dictionary file
    FILE *dict;
    dict = fopen(dictionary, "r");
    // checks to make sure it opens
    if (dictionary == NULL) return false;
    char buffer[LENGTH + 1];
    // sets loop to read while dictionary is not at end of file
    while (fscanf(dict, "%s", buffer) != EOF)
    {
        // allocates memory for new word
        node *currentWord = malloc(sizeof(node));
        // checks to make sure new word has memory
        if (currentWord == NULL) return false;
        strcpy(currentWord->word, buffer);
        currentWord->next = NULL;
        // uses hash function to return value
        int wordHash = hash(currentWord->word);
        wordCount++;
        // insert node into hash table at given location
        // points to first word in linked list first, then points header to this word
        currentWord->next = table[wordHash];
        table[wordHash] = currentWord;
    }
    fclose(dict);
    return true;
}

2 Answers 2

0

I see a couple of problems. First, the hash function can return any number without limit. How many elements are in table[ ]? What happens if the hash function returns a number outside that range? The answer to this is that the resulting behavior is unpredictable. It may or may not result in an error.

Second, there's this: while (cursor->next != NULL) What happens if table[i] is null? Answer- a seg fault because cursor->next doesn't exist. What happens as the code approaches the last two elements in the linked list? Answer: the last element in the linked list would not be checked.

This check shouldn't be for cursor->next, it should be for cursor.

This may be the problem or maybe not. Hard to tell because this isn't all of the related code.

0

Thanks for the help. I fixed those errors, the one that seemed to be causing the bug though was that I needed to change "if (strcasecmp(currentWord, Word))" to "if (strcasecmp(currentWord, Word) == 0)" because it was not comparing the words properly otherwise.

You must log in to answer this question.

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