0

i have been working on the check for many days and not able to find why the misspelled word is more than stuff solution. Rest of the programs looks ok, and i used debug50 ,there is not too much clue. Hope someone can give a help. i also posted on another question since i think we have similar issue. But i did not fully understand that answer and not sure how to change my code accordingly. thanks a lot.

// Implements a dictionary's functionality
#include .....
#define HASHTABLE_SIZE 6000

// define data type
typedef struct node
{
    char word [LENGTH + 1];
    struct node *next;
}
node;

// hashtable
// Ref: https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/
int hash_it(const char *word)
{
    unsigned int hash = 0;
    for (int i = 0, n = strlen(word); i < n; i++)
    {
        hash = (hash << 2) ^ word[i];
    }
    return hash % HASHTABLE_SIZE;
}


// define global variables
node *hashtable[HASHTABLE_SIZE];
unsigned int word_count = 0;



// Returns true if word is in dictionary else false
bool check(const char *word)
{

    //hash the word to get index
    int index = hash_it (word);

    //set up cursor to travel
    node *cursor = hashtable[index];

    // check to the end of the linked list
    while (cursor != NULL)
    {
        if(strcasecmp(word, cursor -> word) == 0)
        {
           return true;
        }
        cursor = cursor -> next;
    }
    return false;
}





// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{

    //open infile dictionary
    FILE *inptr = fopen(dictionary, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Need dictionary, could not open %s.\n", dictionary);
        return false;
    }

    // deinfe variable
    char word[LENGTH+1];
    for (int i = 0; i < HASHTABLE_SIZE; i++)
    {
        hashtable[i] = NULL;
    }


    // scan a new string, put in word
    while (fscanf(inptr, "%s", word) != EOF)
    {
        // hash the word, return index
        int index = hash_it (word);

        // make new node for word
        node * new_node = malloc(sizeof(node));
        if (!new_node)
        {
            unload();
            fclose(inptr);
            return false;
        }
        // copy the word to new node
        strcpy (new_node ->word, word);

        // if hashtable is the first pointer element of arrary point to NULL
        if (hashtable[index] == NULL)
        {
            hashtable[index] = new_node;
            new_node -> next = NULL;
        }
        else
        {
            // insert into linked list
            new_node -> next = hashtable[index];
            hashtable[index] = new_node;
        }

        // word count add
        word_count ++;
    }

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





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




// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    //find each index pointer in the arrary
    for (int i = 0; i <HASHTABLE_SIZE ; i++ )
    {
        node *cursor = hashtable[i];
        while (cursor != NULL)
        {
            node *temp = cursor;
            cursor = cursor -> next;
            free(temp);
        }
    }
   return true;
}
0

I'd assume all your misclassified as misspelled words contain uppercase characters.

In check, you ignore case in comparing the words. But your hash_it doesn't ignore case, so you search the wrong linked list.

One way is to change the hash function to turn everything upper- or lowercase. Or at least remove the difference between the two (for example by applying | 32 or & ~32 to each character).

Another way is to create a lowercase copy of the string, and use that one for hashing and strcmp (no case necessary then).

7
  • i thought strcasecmp does not matter about case anymore? let me add the tolower first anyways. thanks so much – Yao Lu Jul 20 '18 at 8:43
  • strcasecmp ignores the case, yes. But if you're searching in the wrong linked list because the hash function returns different values for you and You, you still won't find anything. – Blauelf Jul 20 '18 at 8:46
  • oh get it, thanks a lot. you are right, i changed that and passed. – Yao Lu Jul 20 '18 at 8:49
  • thanks again However, i run style50 and it says looked good. but when i submit, the style score is 4 out of 5? any clue on that? – Yao Lu Jul 20 '18 at 8:50
  • Does style50 report anything other than "Looks good."? If not, this can be considered a bug, which has happened before, local style50 and server style score should use the same rules. – Blauelf Jul 20 '18 at 10:08

You must log in to answer this question.

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