-1
// Implements a dictionary's functionality

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

#include "dictionary.h"

// creating a node
typedef struct node
{
    char word[LENGTH + 1];
    struct node* next;
}
node;

#define SIZE 179479

// store size of dictionary
int dicsize = 0;
node *hashtable[SIZE];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // TODO
    int wordlen = strlen(word)
    char* lowercase = malloc(sizeof(char) * (wordlen + 1));
    if (lowercase == NULL)
    {
        printf("MISSPELLED WORD\n");
        rturn false;
    }

    // convert letters to a lowercase
    for (int i = 0; i < wordlen; i++)
    {
        if (isupper(wordlen[i]))
        {
            lowercase[i] = tolower(wordlen[i]);
        }
        else
        {
            lowercase[i] = wordlen[i];
        }

        // hash the word
        node* cursor = hashtable[hash(lowercase->word)];

        //check word in dictionary
        while (cursor != NULL)
        {
            if (strcmp(cursor->word, lowercase) == 0)
            {
                free(lowercse);
                return true;
            }
            else
            {
                cursor = cursor->next;
            }
        }
    }
    free(lowercase);
    return false;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // TODO
    // open file dictionary
    FILE* dicnary = fopen(dictionary, "r");
    if (dicnary == NULL)
    {
        printf("could not open %s:\n", dictionary);
        return 1;
    }

    // count word in dictionary
    int word_count = fgetc(dicnary);
    while (word_count != EOF)
    {
        if (word_count == '\n')
        {
            dicsize++;
            word_count = '\0';
        }
    }

    // set file position
    fseek(dicnary, 0, SEEK_SET);

    // allocating memory for each node
    node *new_node = malloc(sizeof(node));

    // scan dictionary word by word
    while (fscanf(dicnary, "%s", new_node->word) != EOF)
    {
        if (new_node == NULL)
        {
            unload();
            return false;
        }
        else
        {
            // copy node into memory
            strcpy(new_node->word, word);
        }

        // hash the word
        unsigned int key = hash(new_node->word, word);
        hashtable[key] = new_node;
    }

    // close dictionary
    fclose(dicnary);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    return dicsize++;
}

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

// Returns hash value, storage location of a given word
unsigned int hash(char* str)
{
    unsigned int sum = 0;
    for (int i = 0; i < str[i] != '\0'; i++)
    {
        sum = str[i];
    }
    return sum % SIZE;
}
8
  • What did you do? What was the expected result? What was the actual result? How did you try to solve the problem this far? --- If you answer those questions, you won't run the risk that we are telling you things that you already know ... – Peter Pesch Oct 22 '18 at 12:59
  • Also, I see lots of TODO's in your code. Does that mean that your code is not finished yet, and that we only have to look at some parts of this code? – Peter Pesch Oct 22 '18 at 13:00
  • And could you please accept some of the answers that Blauelf gave to your previous questions? – Peter Pesch Oct 22 '18 at 13:54
  • I tried to compile the problem with a hash function. The expected results was to get word in dictionary. Now I am on third weeks trying to solve this problem. Please help. Thank you. – Bethuel Mavhungu Oct 23 '18 at 8:23
  • Sorry, I don't understand. If you compile the program, the expected result would have been something like a compiled program, or an executable file. If the expected result is getting a word in a dictionary, the action was running a program, not compiling a program. So could you be more specific: What did you try to do? How did you try to do that? What was the result you were expecting? What was the result you actually saw? At this moment, it is unclear what you are asking ... – Peter Pesch Oct 23 '18 at 8:50
0

Your error message looks like:

 implicit declaration of function 'hash' is invalid in C99 [-Werror,-Wimplicit-function-declaration] unsigned int key = hash(lowercase);

The error message talks about an implicit declaration of a function called hash. So the compiler thinks that you try to declare the function at that point.

You define the function hash at the bottom of your program (which is fine), but you use it before that. The first time you use your function, the compiler should already know what kind of input and output the function needs.

That's why you have to declare the function somewhere before that line.

As your function looks like

// Returns hash value, storage location of a given word
unsigned int hash(char* str)
{
    ...
}

your declaration should look like:

unsigned int hash(char* str);

Finally, to quote Cliff B:

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

6
  • Thank you very much I never realized that. I find it now as easy as abc. – Bethuel Mavhungu Oct 23 '18 at 11:42
  • Now I have another error: dictionary.c:116:49: error: use of undeclared identifier 'word' unsigned int key = hash(new_node->word, word); ^ 2 errors generated. – Bethuel Mavhungu Oct 23 '18 at 11:50
  • Your hash functions takes only 1 input parameter. And you call it using 2 parameters ... That should result in an error ... But before that, you already get another error, saying that the variable word does not exist in function load() ... – Peter Pesch Oct 23 '18 at 12:11
  • I got it. Again thank you very much. – Bethuel Mavhungu Oct 23 '18 at 12:16
  • Please do try to learn to understand error messages. They usually contain lots of useful information ... – Peter Pesch Oct 23 '18 at 12:18

You must log in to answer this question.

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