0
:) dictionary.c, dictionary.h, and Makefile exist
:) speller compiles
:( handles most basic words properly
    expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:) handles min length (1-char) words
:) handles max length (45-char) words
:) handles words with apostrophes properly
:( spell-checking is case-insensitive
    expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:) handles substrings properly
:) program is free of memory errors

From the cs50 checks that I failed it seems like my code is not case-insensitive. Would this be a problem with my hash function or my check function? My full code is posted below.

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.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 unsigned int N = 26;
//max word length
const int MAX_LENGTH = 45;
//counter for number of words
int count = 0;

// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    //hash word to get value
    int hash_value = hash(word);
    //access linked list at hash value
    node *access = table[hash_value];
    //traverse linked list
    while (access != NULL)
    {
        if (strcasecmp(word, access -> word) == 0) //compare to see if word matches any in dictionary
        {
            return true;
        }
        else //move on to next word
        {
            access = access -> next;
        }
    }
    return false; //mispelled word
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int key = tolower(word[0] - 'a');
    return key;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    //open dictionary
    FILE *dic = fopen(dictionary, "r");
    if (dic == NULL)
    {
        return false;
        fclose(dic);
    }
    //initialize hashtable aka fill with garbage values to be filled later
    for (int i = 0; i < N; i++)
    {
        table[i] = NULL;
    }
    //read strings from file
    char word[MAX_LENGTH + 1]; //create array to read word into
    while(fscanf(dic, "%s", word) != EOF)
    {
        node *new_node = malloc(sizeof(node)); //memory for new node
        if(new_node == NULL)
        {
            return false;
            return 1;
        }
        count++; //increase word count for dictionary
        strcpy(new_node->word, word); //copy word into a node
        //hash word
        int head = hash(word);
        //fill hashtable
        if (table[head] == NULL) //if no node is in hashtable
        {
            table[head] = new_node;
            new_node -> next = NULL;
        }
        else //if there is a node in the hashtable
        {
            new_node -> next = table[head];
            table[head] = new_node;
        }
    }
    fclose(dic); //close dictionary
    return true;
}

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

// 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 *temp = cursor;
            cursor = cursor -> next;
            free(temp);
        }
    }
    return true;
}

Thank you for any advice you can give.

1 Answer 1

0

In the sentence: int key = tolower(word[0] - 'a'); you want to 'lowercase' the first character of the word, so you shoud substract 'a' after the 'tolower' operation.

1
  • To clarify, that means that the closing parenthesis is in the wrong place. It should be tolower(word[0]) - 'a';
    – Cliff B
    Aug 10, 2020 at 8:25

You must log in to answer this question.

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