I've been struggling with writing code for case conversions in the check function to make all hash words to be lower-cased. My lack of knowledge in programming overall seems to be revealed at this point..., but I am unsure as to why I might be getting the following error messages: "implicit declaration of function 'itoa' is invalid" and "use of undeclared identifier 'w'". Thank you so much for any help in advance !

// Returns true if a word is in the dictionary, else false

    bool check(const char *word)
       for (int i=0; i < N; i++)
           int w = tolower(word[i]) - 'a';
         node *word_copy = malloc(sizeof(node));
         char *c = itoa(w);
         strcpy(word_copy->word, c);

         int key = hash(c);

         // Setting a cursor variable
         node *temp_word = table[key];
         while (temp_word != NULL)
            if (strcasecmp(temp_word -> word, c) == 0)
               return true;
        temp_word = temp_word -> next;
return false;

1 Answer 1


I see a number of issues here. Let's go through them in order.

First, look at the for loop: for (int i=0; i < N; i++) { int w = tolower(word[i]) - 'a'; }

What are you trying to accomplish here? This code recreates the w var on each pass through the loop and assigns a number between 0 and 25 corresponding to the current letter in word that's being looked at. The problem here is that it doesn't do anything with w after doing that. Also, since w is created/declared inside the loop, it ceases to exist at the end of each pass through the loop, so that value that was calculated is lost.

Next, there's this line:

     char *c = itoa(w);

Did you include the library for itoa? I believe it's stdlib.h. The code error for w happens because w is out of scope and no longer exists, as mentioned above. Also, if you're trying to recreate the word in all lower case, you need more than a char.

That's the two errors, maybe 3 but the more important question is this. Why does the code use itoa at all?

All of the above code makes me think you're trying to convert the chars in the word to numbers and accumulate them somehow, but I really think that you're really just trying to convert all of the letters to lower case in preparation for the hash function. This isn't the way.

A better approach is to do it like this:

  1. Create a string var to hold a string of the same length as the original word. You can do this with a string pointer ( char * c ), but to keep it simple, I recommend the following:

    char word_copy[strlen(word)+1];

This creates a string var of the correct length, including an additional char for the end of string marker. Be sure to set the last char to '\0', the EOS marker.

Do this before the loop in step 2.

  1. In your for loop, copy the lower case version of each letter to the copy.

    word_copy[i] = tolower(word[i]);

Now, you have a word that's all lower case to send to the hash function.

Programming tip: Instead of doing this before calling the hash function, it makes more sense to do it inside the hash function. That way, you can just send the word to the hash function and be done with it.

The reasoning is this: The hash function needs to hash all lower case. If hash is called from more than one place (load and check, in this case), the code doesn't have to be replicated. This guarantees that hash is processing all lower case letters internally. It also eliminates replication of code.

Imagine if you had this hash function in a program where there are dozens or hundreds of calls to hash. Why replicate the prep code before each call? Why not just code it one time inside of hash?

This should get you going. ;-)

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

  • Thank you thank you thank you ! Your point about encompassing the lowercase conversion procedures within the hash function totally makes sense! And looking back on the above code now, my inclusion of the itoa function seems inexplicable to me too since there are no reasons to copy the numbers themselves onto char.. I have not yet settled upon which precise hash function to employ as I seem to spectacularly mess up in adapting even perfectly laid out hash functions on the internet haha.. But your advice has certainly cemented some guiding rocks to my track, so thank you so much!
    – Jae-Min
    Commented Jan 24, 2023 at 18:35

You must log in to answer this question.

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