Thread: Need help with toupper and tolower function

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    30

    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.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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).

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    Then, is it possible to fix that I have so far?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes. In fact, both of us told you how to do so.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    How do I check the current value is a space or not?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    Wow, just like you said. It works now.
    Thanks a lot.
    Last edited by phoebus; 04-27-2008 at 10:20 PM.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    Quote Originally Posted by tabstop View Post
    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 subscribe to a feed

Similar Threads

  1. tolower and locale
    By MK27 in forum C Programming
    Replies: 16
    Last Post: 02-03-2009, 09:05 PM
  2. toupper(); tolower(); ?
    By dune911 in forum C Programming
    Replies: 9
    Last Post: 01-07-2003, 06:40 PM