Need help with toupper and tolower function
This is a discussion on Need help with toupper and tolower function within the C Programming forums, part of the General Programming Boards category; I have this:
Code:
void toggleString(const char *str, char *toggled)
{
while ( *str != '
-
Need help with toupper and tolower function
I have this:
Code:
void toggleString(const char *str, char *toggled)
{
while ( *str != '\0') {
if ( islower( *str ) ) /* if character is lowercase */
*toggled = toupper ( *str ); /* convert to uppercase */
else if ( isupper ( *str) )
*toggled = tolower ( *str );
++str; /* move str to the next character */
++toggled;
}
} I want to change lower letters to upper and vice versa in a string. However, the code only work for first word and skip the rest. Could someone give me a hint?
Thanks.
-
Jack of many languages
You're not terminating the string at toggled.
If the character is not uppercase or lowercase, (like maybe a blank), you're not copying it, and thus advancing toggled pointer over an arbitrary (and possible null terminating) character.
Todd
Mac and Windows cross platform programmer. Ruby lover.
Quote of the Day
12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.
Amen brother!
-
and the Hat of Guessing
When the current value of *str is not a letter, you don't copy it over to toggled. Hence whatever was already there stays (since you skip over it with ++toggled).
-
Then, is it possible to fix that I have so far?
-
and the Hat of Guessing
Yes. In fact, both of us told you how to do so.
-
How do I check the current value is a space or not?
-
and the Hat of Guessing
To answer your literal question: isspace. (Actually any whitespace, including tabs.) To answer the question you're actually asking: if it's not isupper and it's not islower, then it's some sort of something else, punctuation or whatever. So just copy it.
-
Wow, just like you said. It works now.
Thanks a lot.
Last edited by phoebus; 04-27-2008 at 10:20 PM.
-

Originally Posted by
tabstop
To answer your literal question: isspace. (Actually any whitespace, including tabs.) To answer the question you're actually asking: if it's not isupper and it's not islower, then it's some sort of something else, punctuation or whatever. So just copy it.
Wow, just like you said. It works now.
Thanks a lot.
Popular pages Recent additions
Similar Threads
-
By MK27 in forum C Programming
Replies: 16
Last Post: 02-03-2009, 08:05 PM
-
By dune911 in forum C Programming
Replies: 9
Last Post: 01-07-2003, 05:40 PM