0

Can somebody please help me whenever i execute check50 on my code i get this

:( 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..."
:| program is free of memory errors
can't check until a frown turns upside down

my code:

// Implements a dictionary's functionality

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


#include "dictionary.h"

// Represents number of buckets in a hash table
#define N 26
int quantity = 0;


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

// Represents a hash table
node *hashtable[N];

// Hashes word to a number between 0 and 25, inclusive, based on its first letter
unsigned int hash(const char *word)
{
    return tolower(word[0]) - 'a';
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Initialize hash table
    for (int i = 0; i < N; i++)
    {
        hashtable[i] = NULL;
    }


    // Open dictionary
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        unload();
        return false;
    }

    // Buffer for a word
    char word[LENGTH + 1];
     node* head = NULL;

    // Insert words into hash table
    while (fscanf(file, "%s", word) != EOF)
    {
        int position = hash(word);
        node* a_node = malloc(sizeof(node));
        if (hashtable[position] == NULL)
        {
            hashtable[position] = a_node;
            strcpy(a_node->word , word);
            a_node->next = NULL;
        }
        else
        {
            strcpy(a_node->word,word);
            a_node->next = hashtable[position];
            hashtable[position] = a_node;
        }
        quantity++;
    }

    // Close dictionary
    fclose(file);

    // Indicate success
    return true;
}

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

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

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
   node *start = malloc(sizeof(node));

    for (int i = 0; i < N; i++)
    {
        start = hashtable[i];
        node *move = start;

        while (move != NULL)
        {
            node *temp = move;
            move = move->next;
            free(temp);
        }
    }

    free(start);

    return true;
}
2

This test in check if(temp->next != NULL) is the heart of the problem. What is the value of temp->next if there is only one word in the "position"? It is, of course, null. Realize that check50 doesn't use the "large dictionary" (you can detect that by looking at the check50 log). Don't you want to continue into the while loop when hastable[position] is not null? There may be other problems in the code, but fixing this should get you further along.

1
  • thanks! made a couple of changes in the conditionals and the program worked except it did show some valgrind errors. i guess thats more work for me. thanks again i was stuck on this for a long time. Jul 28 '19 at 18:27
0

Handles words with apostrophe correctly: This error comes because they want you to display the words exactly the way it was given in the text file. Suppose fOo was the word in text file which was misspelled. What your program might be doing is that it prints foo and not fOo as the misspelled word. They want you to preserve the case of the letters! This error comes at the time when you had converted your word to lowercase( at the time of comparing it with the dictionary words). That is, fOo has now changed to foo. So, you can instead assign the value of word to some other string by using strcpy() and then do comparison of dictionary words with duplicate string( which is lowercase version of original string or word).

1
  • What you have said are general descriptions of what each test is looking for and generalities of what might be wrong. These statements may be correct, but as an answer, this doesn't provide any particularly useful insight into what is wrong with the code. When all of the tests are failing, as is the case here, there's usually one central underlying problem that has to be resolved. This is what @DinoCoderSaurus has provided. You might find it useful to study answers that have been accepted and note the specificity of their content.
    – Cliff B
    Jul 28 '19 at 20:57

You must log in to answer this question.

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