0

I've implemented load and check in pset5. It works, but one-letter words "a", "A" and "I" are also coming up misspelled along with the words that are correctly identified as misspelled words. My code is below. Thanks for any help you can offer.

/**
 * dictionary.c
 *
 * Computer Science 50
 * Problem Set 5
 *
 * Implements a dictionary's functionality.
 */

#include <cs50.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "dictionary.h"

/*
* in dicitonary.h
*
* #define LENGTH 45
*
* typedef struct node
* {
*    char dict_word[LENGTH + 1];
*    struct node* next;
* }
* node;      
*/

// global variables for each function
// initiate hashtable
node* hashtable[26] = {NULL};

// hashed number
int h_num;

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{
// length of word plus '/0' character
int z = strlen(word) + 1;

// storage for word to be checked
char str[z]; // = malloc(sizeof(z));

// convert each letter to lowercase
for (int y = 0; y < z; y++)
{
    if (isupper(word[y]))
    {
        str[y] = tolower(word[y]);
    }

    else
    {
        str[y] = word[y];
    }
}

// hash function
h_num = str[0] - 'a';

// temporary node pointer to search hashtable lists
node* trav = hashtable[h_num];

while (trav->next != NULL)
{

    if (strcmp(str, trav->dict_word) == 0)
    {
        return true;
    }

    else
    {
        trav = trav->next;
    }
}

return false;

// currently printing 'a' 'A' and 'I' in addition to misspelled words

}

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
// open dictionary "d" for reading
FILE* d = fopen(dictionary, "r");

// Error checking
if (d == NULL)
{
    printf("Could not load dictionary.\n");
    return false;
}


while (!feof(d))
{
    // array to hold the words of the text file
    char str[LENGTH + 1];

    // read the words of the text file
    fscanf(d, "%s", str);

    // simple hash function uses first letter of word to sort
    h_num = str[0] - 'a';

    // first time the hash function spits out a particular pointer, put it in the head node
    if (hashtable[h_num] == NULL)
    {
        // use strcpy because arrays do not support assignment (=) operator
        hashtable[h_num] = malloc(sizeof(node));

        if (hashtable[h_num] == NULL)
        {
            printf("Error in memory allocation for initial node hashtable[%i]\n", h_num);
            free(hashtable[h_num]);
            return 1;
        }

        strcpy(hashtable[h_num]->dict_word, str);
        hashtable[h_num]->next = NULL;
    }

    // every subsequent time, point node to previously used node
    else
    {
        node* newNode = malloc(sizeof(node));

        // error check to make sure we haven't run out of memory
        if (newNode == NULL)
        {
            printf("Error in memory allocation\n");
            free(newNode);
            return 1;
        }

        // again, use strcpy because arrays do not support assignment (=) operator
        strcpy(newNode->dict_word, str);

        // point newNode's pointer to the original hash's node
        newNode->next = hashtable[h_num];

        // point the hashtable pointer to the newest created node
        hashtable[h_num] = newNode;
    }

}

fclose(d);    
return true;
}
1

You have to consider the case, where the word you try to check is the last word in your linked list. In that case trav->next will be NULL, but since you don't enter the while loop, you never check if the word you have matches the stored one, and you return false.

I'm not sure that's the only mistake. Correct it and let me know in the comments!

Till then, Happy Coding! :)

2
  • 1
    Damn, thanks. I'm gonna work on that and report back.
    – Scott
    Aug 25 '16 at 1:37
  • In check, changed my while condition from "while (trav->next != NULL)" to "while (trav != NULL)" and it now works completely. Thanks!
    – Scott
    Aug 27 '16 at 1:45

You must log in to answer this question.

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