0

for Speller in Pset 5, I am having issues with my check function, not sure how to deal with words with apostrophe and need some tips if possible. The issue should be with the check function of my code, when comparing with keys I loaded the same number of words for the dictionary. However, when using check50, it said that my code has issues with basic words but I cant seem to reproduce them when I use a text file of my own. Below is my code for check as well as hash.

// Implements a dictionary's functionality

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

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

// TODO: Choose number of buckets in hash table
const unsigned int N = 26;

// Hash table
node *table[N];
int counter = 0;
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    //if apostrophe...

    int hcode = hash(word);

    node *trav = table[hcode];
// set to  start of hash table

   while (trav!=NULL)
    {
        if (strcasecmp(word,trav->word)==0)
        {
            return true;
        }

        trav = trav->next;

    }


    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{   int first = 0;
    // TODO: Improve this hash function
    // return 0-25 according to first letter of the word
    char lower = tolower(word[0]);

    first = lower - 97;
    return first;
}

// Loads dictionary into memory, returning true if successful, else false string input represents dictionary file name
bool load(const char *dictionary)
{


    FILE *dict = fopen(dictionary, "r");
    char tmpword[LENGTH +1];
    //allocate space for the words


    int hcode;

    while (fscanf(dict, "%s", tmpword)!=EOF)
    {



         node *n = malloc(sizeof(node));
        //after 2 words in dictionaries small, cant end fscanf returns 0 value
        strcpy(n->word, tmpword);
        hcode=hash(tmpword);

        n->next = table[hcode];
        table[hcode]=n;
        counter++;
    }

    fclose(dict);
    return true;

}

// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size()
{

    return counter;
}

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
    //go through each path

    for (int i =0; i<N;i++)
    {
         node *cursor =table[i];
         node *tmp = table[i];
        while (cursor->next!=NULL)
        {
            cursor = cursor->next;
            free(tmp);
            tmp = cursor;
        }
         free(cursor);
    }

    //check if all unloaded

    return true;
}

1 Answer 1

0

[EDIT - previous answer totally unrelated to the problem.]

Now that I have the full code to test, the problem became quickly obvious. The full code produces a segmentation fault when executed. This causes the program to terminate prematurely. That's why check50 is failing every test. If you notice, the check50 results are showing the misspelled words correctly. You need to learn to interpret faulty program output very carefully, to determine what is wrong and how far the program got before it crashed.

The problem lies in the unload function. If the code were to simply return true, most of the tests would pass. Only the valgrind test would fail because the dictionary would not actually be unloaded.

I'm truly surprised that you didn't get any seg faults when you ran it locally.

Now that you know where to look for the problem, I'll give you a chance to run it and see if you can resolve the issue. It's a common problem reported on this pset.

Hint - think about exactly what every line of code does. Don't assume its right because you saw it somewhere. ;-)

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

5
  • I see. I thought that apostrophes would cause an issue when using strcasecmp as the words in the dictionary would not have any apostrophes. I have changed my do while loop to a while loop per your advice but I still have issues with check50. I added my load, unload and size functions to my original post but don't think they should have any issues as I am able to load the same number of words as the key texts.
    – duckpro
    Commented Jan 17, 2023 at 3:01
  • please add EVERYTHING - including all the declarations at the beginning of the file.
    – Cliff B
    Commented Jan 17, 2023 at 7:51
  • Ok I have included all my code for dictionary.c.
    – duckpro
    Commented Jan 17, 2023 at 14:42
  • see my new answer
    – Cliff B
    Commented Jan 18, 2023 at 1:12
  • OH I see the issue with the while loop in unload, now the check50 results make more sense. I have no clue why there were no segmentation faults when I ran it, even tried valgrind and could not find anything. Thanks again for the help!
    – duckpro
    Commented Jan 18, 2023 at 13:02

You must log in to answer this question.

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