0

Was hoping someone could help me figure out why I'm getting all words misspelled with my code in pset5. I've done load and check so far and can't see where the problem is.

Here's a link to pastebin (http://pastebin.com/11YiAr9K), and it's also copied below.

Thanks for any help you can give.

    /**
 * 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 dictionary.h

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

// 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 = tolower(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 every word, misspelled or not

 }

 /**
  * 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)
    {
        // malloc space for node
        hashtable[h_num] = malloc(sizeof(node));

        // check to see if we're out of memory
        if (hashtable[h_num] == NULL)
        {
            printf("Error in memory allocation for initial node hashtable[%i]\n", h_num);
            free(hashtable[h_num]);
            return 1;
        }

        // use strcpy because arrays do not support assignment (=) operator
        // puts str into the dict_word (first piece of node)
        strcpy(hashtable[h_num]->dict_word, "str");

        // pointer in node points to NULL
        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
        // insert new dictionary word into newNode
        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 Answer 1

1

At a quick glance, I see one very serious error that results in only one word in the dictionary - "str" - and nothing else. Look at the following:

    strcpy(hashtable[h_num]->dict_word, "str");

By surrounding str with double quotes, you are telling strcpy to copy the literal string "str" into dict_word, not the contents of the string array str. If it had been "fred", then fred would have been copied. Whatever is between the double quotes will be copied, it will not be treated as a variable or a pointer or memory location or any other kind of storage.

Lose the double quotes.

There may be other problems, but I think I'll let you have a crack at checking and debugging them first. ;-)

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

1
  • Yes, thank you. Can't believe it was something that small. Still getting two misspelled words that I shouldn't (capital 'I' and lowercase 'a') but I'm gonna investigate on my own and consider this question answered. Thank you again!
    – Scott
    Commented Aug 21, 2016 at 15:25

You must log in to answer this question.

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