0

Below, I want to turn each letter in the array of word into lower case. I'm using a loop and tolower to change each letter. But I get this error when I compile:

dictionary.c:44:33: error: read-only variable is not assignable word[i] = tolower(word[i]);

Per the pset instructions, I can't change the type of word. Thoughts?

bool check(const char* word)
        {

            // Hash the word
            int index = 0;
            if(word[0] == '\'')
            {
                index = HASHLENGTH;
            }
            else
            {
                index = (tolower(word[0]) - 'a') % HASHLENGTH;
            }    

            // Initialize the return value    
            bool result = false;

            for(node* cur = HASHTABLE[index]; cur != NULL; cur = cur->next)
            {
                // turn to lower case
                for(int i = 0; i < strlen(word); i++)
                {
                    word[i] = tolower(word[i]);
                }


                if(strcmp(cur->word, word) == 0)
                {
                    result = true;
                    break;
                }        
            }

            return result;
        }
0

Easy solution. The key here is that word is fixed as a constant in bool check(const char* word). That means that it cannot be changed, period. However, there's nothing preventing you from copying the contents of word into another local string variable and altering, comparing, or otherwise operating on it.

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

  • I had tried that (basically doing char* temp = word) and got the error below. Also thought maybe I'm not able to set them equal to each other so did strcpy(word temp) and that didn't work either. Error for first version: dictionary.c:26:11: error: initializing 'char ' with an expression of type 'const char *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers] char temp = word; – Norcim133 Dec 28 '15 at 1:31
  • with char* temp = word, all you have done is assigned a pointer to the same immutable object. You need to do something like char word2[strlen(word)]; and then strcpy(word2, word); You need to make an actual, physical copy of the original, not just another pointer to the same object. – Cliff B Dec 28 '15 at 1:47
  • I get it. I had done what you tried but incorrectly. I had initialised the value of temp to NULL. So when I did strcpy it didn't have anyplace to copy it. My workaround for that had me setting the temp = word (i.e. to the same pointers) which created other issues. – Norcim133 Dec 28 '15 at 1:48
  • got bit by the fact that when you create a string, you fix the length, did you? ;-) – Cliff B Dec 28 '15 at 1:49

You must log in to answer this question.

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