1

My code is able to pass all the tests in check50 except the valgrind one. Here is the error when I run the valgrind. Could someone help me, please?

enter image description here

// Implements a dictionary's functionality
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>

#include "dictionary.h"

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

// Number of buckets in hash table

const int N = 26 * 26 * 26 -1;

// Hash table
node *table[N];


// Returns true if word is in dictionary else false
bool check(const char *word)
{
    char lower_word[LENGTH + 1];
    for (int i = 0; i <= strlen(word); i++)
    {
        lower_word[i] = tolower(word[i]);
    }
    node *cursor = table[hash(lower_word)];
    while (cursor != NULL)
    {
        if (strcasecmp(cursor -> word, word) == 0)
        {
            return true;
        }
        else
        {
            cursor = cursor -> next;
        }

    }
    return false;
}

// Hashes word to a number
// My hash function is based on the first three letter of the word. If there is no 2nd or 3rd letter, I treat the 2nd or 3rd letter an "a".
unsigned int hash(const char *word)
{
    int character_1 = 0;
    int character_2 = 0;
    int character_3 = 0;
    if (word[0] < 65 || (word[0] <= 96 && word[0] >= 91) || word[0] >= 123)
    {
        character_1 = 0;
    }
    else
    {
        character_1 = (int) (word[0] - 97);
        character_2 = 0;
        character_3 = 0;
        if (word[1] != '\0' && (word[1] >= 65 && word[1] <= 90) && (word[1] >= 97 && word[1] <= 122))
        {
        character_2 = (int) (word[1] - 97);
        }
        else
        {
            character_2 = 0;
            character_3 = 0;
        }

        if (word[2] != '\0' && (word[2] >= 65 && word[2] <= 90) && (word[2] >= 97 && word[2] <= 122))
        {
            character_3 = (int) (word[2] - 97);
        }
        else
        {
            character_3 = 0;
        }
        }

    return (character_1 * 26 * 26 + character_2 * 26 + character_3);

}



bool loaded = false;
unsigned long counter = 0;

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    char buffer[LENGTH + 1];
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        //unload();
        return false;
    }
    while (fscanf(file, "%s", buffer) != EOF)
    {
        node *newword = malloc(sizeof(node));
        if (newword == NULL)
        {
            unload();
            return false;
        }
        else
        {
            counter += 1;
            strcpy(newword -> word, buffer);
            newword -> next = NULL;
            int bucket_num = hash(buffer);
            if (table[bucket_num] == NULL)
            {
                table[bucket_num] = newword;
            }
            else
            {
                newword -> next = table[bucket_num];
                table[bucket_num] = newword;
            }
        }
    }
    fclose(file);
    loaded = true;
    return true;
}


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

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

0

The valgrind error(s) come from one letter words because word[2] is not initalized.

You must log in to answer this question.

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