0

I have the dictionary loaded, and the first part of check done but -

  1. I cannot move the cursor properly.
  2. strcmp is not working properly. I tested it alone in another file and it did work. It is just not here.

It is in the block near the bottom that I think the problem is, moving through the list.

if(strcmp(arr, head->word) == 0)
          {
             puts("equal");
             return 0;
          } 
 //try to set the cursor to it's own next value (not sure if U can do this?)

      1.    cursor = cursor->next;

                   OR
  //keep moving head and pointer cursor to head ->next
      2.    cursor = head -> next
  //reset head to 2nd node, and so on
            head = cursor;

Both of the two attempts to move the cursor print out the same values.

The dictionary prints fine with ethier syntax

printf("dictionary word:%s\n",cursor->word)

But the text is not working. It prints each word multiple times. Like 100x each. No idea why.

printf("text word:%s", arr)

Check() function

     #include "dictionary.h"

    int hashValue(const char *word);


    typedef struct node
     {
     char word[LENGTH + 1];
    struct node *next;
     }
     node;

    node *hash_table[HASHTABLE_SIZE] = {NULL};

    // Returns true if word is in dictionary else false
    bool check(const char *word)
    {
       char arr[LENGTH+1];

     // HASH VALUE
    //     hash to get bucket/array index
      int hash_index = hashValue(word);
    // set head to first node in bucket/array
     node *head = hash_table[hash_index];

    // SET WORD TO LOWER
     for(int i = 0; i < strlen(word);i++)
     {
    arr[i] = tolower(word[i]);
      }
      // printf("string: %s\n", arr);
     // set cursor originally to head

       node *cursor = head;
      while(cursor != NULL)
    {

      if(strcmp(arr, head->word) == 0)
      {
         puts("equal");
         return 0;
      }
        cursor = head->next;
        printf("%s\n", arr);
        head = cursor;

    }
    return true;
}

Full file with load() if this if needed https://gist.github.com/chrisdel101/f34af62bd75454fe8511ec3750498556

1

There are a number of issues in the code that are causing issues. Here are the ones I spotted, there are probably more.

Check:

The word is being hashed without converting to all lower case. If there's a capital letter, it will return a different hash value.

The end of string marker isn't being stored in arr.

If strcmp results in a match, it returns 0, which is correctly checked. HOWEVER, the code then goes on to return 0;. Check is supposed to return a bool. When a number is returned, a 0 is treated as false and a non-zero is treated as true. That means that when the code finds a match, the code returns false. ALWAYS return true or false for a bool, never a number!

Hash function:

return hash % sizeof(HASHTABLE_SIZE);

The value sizeof(HASHTABLE_SIZE) returns the size of the constant HASHTABLE_SIZE, not the value that it represents. In other words, it will always be 4. You probably want this:

return hash % HASHTABLE_SIZE;

There are other issues, but its important that you work through the debugging process and sharpen your skills. If, after fixing these and working on it further, you get stuck, please post a new question.

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

3
  • I was getting garbage values at the end of the strings since the end of string marker isn't being stored in arr. I don't get this at all. I had to look at someone else's post to see how to do it. Using int len = strlen(word); char arr[len+1]; arr[len+1] = '\0'; did not work. Instead, arr[len] = '/0'worked. No more garbage. Not sure why it's not [len+1] to be put at the end. How would we know to do this end marker?? Redirecting to a resource I can read would be an helpful if U know any. I never picked up on this need for an end marker from the walkthrough or the instructions :)
    – Mote Zart
    May 1 '18 at 2:31
  • I made the other changes. Put the lowercased array into the hash function, fixed the hash function sizeof() issue, and changed return from 0 to true.
    – Mote Zart
    May 1 '18 at 2:34
  • The reason is simple. Say the word is "cat". Then strlen("cat") = 3, but you need an array with 4 positions to properly store it. That means that cat will go into positions 0, 1 and 2 of an array. The EOS marker goes into position 3. Remember, an array (including char arrays) starts at 0, not 1.That's also why the size of an array is always strlen +1, to allow for the \0.
    – Cliff B
    May 1 '18 at 2:42

You must log in to answer this question.

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