0

my code does not work so well because it could not unload the dictionary/large eventhough there are no leaks in memory. My programm does also identify the misspelled words wrongly.

Just comment if you have answer or questions about my code. Thanks for your help alot!

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

    // Represents number of buckets in a hash table
    #define N 26

    // 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];

    int wordcount;


    // 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];
        wordcount = 0;
        // Insert words into hash table
        while (fscanf(file, "%s", word) != EOF)
        {
            // TODO
            node *new_node = malloc(sizeof(node));
            if (new_node == NULL)
            {
                unload();
                return false;
            }

            strcpy(new_node->word, word);
            int hashed = hash(word);
            if (hashtable[hashed] == NULL)
            {
                hashtable[hashed] = new_node;
                new_node->next = NULL;
            }
            else
            {
                new_node->next = hashtable[hashed];
                hashtable[hashed] = new_node;
            }
            wordcount++;
        }

        // 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)
    {
        // TODO
        return wordcount;
    }

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

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

        }
        return true;
    }
1

1 Answer 1

3

The errors give a very good clue to the problem. Program is "case sensitive" and must be "case insensitive". Perhaps investigate how you can use strcasecmp (as Zamyla mentions in the check walkthrough). From the man page [emphasis added]:

The strcasecmp() function compares the two strings s1 and s2, ignoring the case of the characters. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

3
  • Hi, thanks alot for your comment! thanks to you there is no more error according to check50, however the program still does not work correctly as dictionary could not be unloaded, following are some lines in implement box: $ ./speller texts/lalaland.txt MISSPELLED WORDS .... IRIS FADE OUT THE END Could not unload dictionaries/large. Feb 14, 2019 at 22:34
  • Something doesn't jive. Do you have multiple version of the source? What is implement box? IRIS FADE OUT THE END are all words in supplied dictionaries/large so would not be "misspelled" (with the case compare correction). Feb 15, 2019 at 0:28
  • sorry I meant command line terminal, someone told me that maybe I changed something in either dictionary or speller.c, ill take a look on my own, thanks for your help! Feb 15, 2019 at 22:50

You must log in to answer this question.

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