C help, tolower()

Joined
Jan 21, 2004
Messages
561
I need to make this string lowercase but I can't seem to get it working

Code:
void inputBooks()
{
	struct BOOKS info[]

	// ASK for input
	printf("Enter the name of the book: ");
	gets(info[0].name);

        ...........

How can I use tolower() here? I tried this
Code:
void inputBooks()
{
	struct BOOKS info[]
        char temp_string;

	// ASK for input
	printf("Enter the name of the book: ");
	gets(&temp_string);
        // fill the structure thing
        info[0].name = tolower(temp_string);
  
        ...........
I get an "illegal operand" error on the ; after the tolower()


thanks for any help
 
Hmm you should be able to do something like this:

Code:
char* p = info[0].name; // I'm assuming name is a character array
while (*p)
    tolower(*p++);
 
yeah, the problem is that its expecting a character and you're passing it an array of characters. essentially, you've got a type mismatch.

the fix provided should work. it loops through and converts each character in the string if needed.

if you can't get it to work for some odd reason, you could do something like

Code:
int t=0;
char temp=stringy_thingy[t];

while (temp){
   if (temp>='A' && temp <='Z')
      stringy_thingy[t] +=  32;

//from memory the distance between lower case and capital letters is 32 in the ASCII table
//if it isn't, you will need to adujst the number accordingly.

   temp = stringy_thingy[++t];
   
}
 
nameless_centurian said:
//from memory the distance between lower case and capital letters is 32 in the ASCII table
//if it isn't, you will need to adujst the number accordingly.
It's 32, but the uppercase letters are lower than the lowercase letters.

This kind of question is moot, though, if you do a little analysis: What's the constant? 32, but why use a constant; why not just add ('a' - 'A') and let the compiler sort it out? Why do the math yourself instead of using tolower()? Why do the test yourself when you could use isupper()? Why go character by character when you could use strlwr()?
 
mikeblas said:
It's 32, but the uppercase letters are lower than the lowercase letters.

This kind of question is moot, though, if you do a little analysis: What's the constant? 32, but why use a constant; why not just add ('a' - 'A') and let the compiler sort it out? Why do the math yourself instead of using tolower()? Why do the test yourself when you could use isupper()? Why go character by character when you could use strlwr()?

have mercy on a self-proclaimed beginner!

great points. i hope to someday get to think in terms of what is available to use instead of simply what the problem is.
 
nameless_centurian said:
i hope to someday get to think in terms of what is available to use instead of simply what the problem is.
You'll get there; just keep plugging away.
 
Sweet, thanks a ton guys.
In the end this is what I got:

Code:
void inputBooks(struct BOOKS info[])
{   
    // ASK for input
    printf("Enter the name of the book: ");
    gets(info[0].name);
    lowercase(info[0].name);

.....................


void lowercase(char string[])
{
   int  i = 0;

   while ( string[i] )
   {
      string[i] = tolower(string[i]);
      i++;
   }

   return;

}
 
Back
Top