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 != '

Thread: Need help with toupper and tolower function

') { if ( islower( ...

  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
    Katy, Texas
    Posts
    2,309
    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!

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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21