0

check50 show:

 $check50 cs50/2017/x/speller
:) dictionary.c, dictionary.h, and Makefile exist
:) speller compiles
:( handles most basic words properly
expected "MISSPELLED WORD...", not "MISSPELLED WORD..."
:( handles min length (1-char) words
expected "MISSPELLED WORD...", not "MISSPELLED WORD..."
:( handles max length (45-char) words
expected "MISSPELLED WORD...", not "MISSPELLED WORD..."
:( handles words with apostrophes properly
expected "MISSPELLED WORD...", not "MISSPELLED WORD..."
:( spell-checking is case-insensitive
expected "MISSPELLED WORD...", not "MISSPELLED WORD..."
:( handles substrings properly
expected "MISSPELLED WORD...", not "MISSPELLED WORD..."

From the detail(https://cs50.me/checks/8ce060d76644e6d455815106af647471840b455e), it says unload is the issue. But I don't find the bug in my unload code. Here is my full code:

/**
* Implements a dictionary's functionality.
*/

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

#include "dictionary.h"


/**
 * Create a node *
 */
typedef struct node
  {
    char word[LENGTH+1];//LENGTH is already defined in dictionary.h file
    struct node *next;
}
node;


/**
*create a hashtable using node *
*/
node *hashtable[27];


/**
 * Create hash function
 */
    int hash(const char* word)
    {
        int index;
        if(isupper(word[0]))
        {
            index = word[0] - 'A';
            return index;
        }
        else
        {
            index = word[0] - 'a';
            return index;
        }

    }

   /**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
int index = hash(word);
//create a pointer which only point to the head node
 node *cursor = hashtable[index];

//traverse the linked list
while(cursor != NULL )
{
    if (strcasecmp(cursor->word, word) == 0)
    {
        return true;
    }
    else
    {
        cursor = cursor->next;
    }
}
printf("MISSPELLED WORD");
return false;
}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{
//initiate hashtable
for (int i = 0; i < 26; i++)
{
    hashtable[i] = NULL;
}

// open dictionary file
FILE *fp = fopen(dictionary, "r");
if (fp == NULL)
{
      printf("Could not open %s.\n", dictionary);
      return 1;
}

// scan dictionary word by word
char new_word[LENGTH+1];
int index;
while(fscanf(fp,"%s", new_word) != EOF)
{
    //malloc a node * for each new word
    node *new_node = malloc(sizeof(node));
    if (new_node == NULL)
    {
        //check if malloc() succeed whenever we create a new node
        unload();
        return false;
    }
    else
    {
        // if succeed, then copy word into node
        strcpy(new_node->word, new_word);
        // insert node into a linked list
        index = hash(new_node->word);
        new_node->next = hashtable[index];
        hashtable[index] = new_node;
    }
}
fclose(fp);
return true;
   }



/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */
unsigned int size(void)
{
int n = 0;
for(int j = 0; j < 26; j++)
{
    node *cursor = hashtable[j];
    if(cursor == NULL)
    {
        return 0;
    }
    else
    {
        while(cursor != NULL)
        {
            n++;
            cursor = cursor->next;
        }
    }
}
return n;
}

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{
for (int i = 0; i < 26; i++)
{
    node *cursor = hashtable[i];
    if (cursor == NULL)
    {
        return false;
    }
    else
    {
        while(cursor != NULL)
        {
        node *temp = cursor;
        cursor = cursor->next;
        free(temp);
        }

    }

}
return true;
}
1

Look at the following code:

node *cursor = hashtable[i];
if (cursor == NULL)
{
    return false;
}

If there's a hash number that didn't have any words, which is possible, especially with a small dictionary like the ones used in check50, the unload function terminates and returns false. It should just move on to the next hash number.

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

2
  • Yes. I fix it. Add some code to both unload and size functions since they have the same problem in my original code. Thanks a lot. I start getting more and more sense of the importance of logic and carefulness of writing code to solve a problem. Appreciate your help.
    – juanli
    Oct 15 '17 at 7:49
  • believe i have the same issue here, however, even after reading above, i am not sure how to fix my code, any more clue pls?
    – Yao Lu
    Jul 20 '18 at 3:39

You must log in to answer this question.

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