Thread: How to convert the first character of a std::string to lowercase...

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    How to convert the first character of a std::string to lowercase...

    hi,

    I'm trying to convert the first character of a std::string to lowercase. I tried using Tolower, but it doesn't work :-(

    example:
    Code:
    string nEq1 = "Sporting", nEq2 = "Porto", idPlayer;
    
    idPlayer = nEq1.at(0);
    idPlayer += "1";
    
    idPlayer //transform the first character of this std::string to lowercase????
    Can anyone help me?
    Last edited by IndioDoido; 11-19-2007 at 05:29 PM.
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You say you tried using tolower? Let's see what you tried.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi hk_mp5kpdw...

    i tried this:
    Code:
    idPlayer.ToLower();
    and this:
    Code:
    transform (idPlayer.begin(), tolower);
    and both don't work and give me errors :-(
    "Artificial Intelligence usually beats natural stupidity."

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What were the errors? Where did you get the idea for that second bit of code? Go back and look at it again, you just didn't follow the example fully. The first try won't work because ToLower() is not a function of the string class.

  5. #5
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ups! i'm really lame :-(

    i forgot the:
    Code:
    #include <algorithm>
    after including algorithm, VS2005 was able to recognize the transform() and help me do what i really wanted to do...
    Code:
    transform(idPlayer.begin(), idPlayer.end(), idPlayer.begin(), tolower);

    Thanks anyway for the help...
    I'll be more careful next time i ask for help.
    "Artificial Intelligence usually beats natural stupidity."

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by IndioDoido View Post
    ups! i'm really lame :-(

    i forgot the:
    Code:
    #include <algorithm>
    after including algorithm, VS2005 was able to recognize the transform() and help me do what i really wanted to do...
    Code:
    transform(idPlayer.begin(), idPlayer.end(), idPlayer.begin(), tolower);

    Thanks anyway for the help...
    I'll be more careful next time i ask for help.
    I'm surprised that even compiled. tolower should be an overloaded function, so it needs a typecast. But even then that algorithm converts the entire string.

    But you say you want to convert only the first characters, not the entire string. This is a three step process:
    1)Obtain the character to be converted from the string.
    2)Convert the character to lowercase
    3)Replace the old character with the converted one.

    That's the pseudo code, now write that in C++.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi King Mir...

    I'm surprised that even compiled. tolower should be an overloaded function, so it needs a typecast. But even then that algorithm converts the entire string.
    Yap...it didn't give me any compile error :S

    I'm only copying the first character from the string...
    Code:
    string nEq1 = "Sporting", nEq2 = "Porto", idPlayer;
    
    idPlayer = nEq1.at(0);
    and only then i'm converting it to lowercase.

    By the way...if i've wanted something like this:
    "SPORTING"
    and transform into:
    "sPORTING"

    Wouldn't it be possible with:
    Code:
    transform(idPlayer.begin(), idPlayer.end(), idPlayer.begin(), tolower);
    ??
    "Artificial Intelligence usually beats natural stupidity."

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Just use tolower on that character. Note that at() returns a char reference, so you need to use tolower directly on the result of at(0) or save it in a reference to char variable so that it will update that value in the string.
    Last edited by Daved; 11-19-2007 at 11:36 PM.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by IndioDoido View Post
    By the way...if i've wanted something like this:
    "SPORTING"
    and transform into:
    "sPORTING"

    Wouldn't it be possible with:
    Code:
    transform(idPlayer.begin(), idPlayer.end(), idPlayer.begin(), tolower);
    ??
    You are passing begin...end to the transform() function, so why would you expect it to only affect the first character? It's going to affect the entire range. You COULD do:

    Code:
    transform(idPlayer.begin(), idPlayer.begin(), idPlayer.begin(), tolower);
    But that's a bit ridiculous to operate on a single character. If you want to change the first character, just do it.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by brewbuck View Post
    You are passing begin...end to the transform() function, so why would you expect it to only affect the first character? It's going to affect the entire range. You COULD do:

    Code:
    transform(idPlayer.begin(), idPlayer.begin(), idPlayer.begin(), tolower);
    But that's a bit ridiculous to operate on a single character. If you want to change the first character, just do it.
    You mean:
    Code:
    transform(idPlayer.begin(), ++idPlayer.begin(), idPlayer.begin(), tolower);
    Or possibly saving idPlayer.begin() to a variable and incrementing that.

    Otherwise you're operating on an empty list.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Oh, dear. std::transform works on a range. You want to change just one character, not a range.

    Assign the result of calling tolower on the first character of the string to the first character?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by King Mir View Post
    You mean:
    Code:
    transform(idPlayer.begin(), ++idPlayer.begin(), idPlayer.begin(), tolower);
    Or possibly saving idPlayer.begin() to a variable and incrementing that.

    Otherwise you're operating on an empty list.
    Yep. Bug!

  13. #13
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    wow, thanks alot guys!

    I really didn't now how to use transform().
    In C i simply used strlwr(), but now i have a pretty good idea on how transform() works in this case :-)
    "Artificial Intelligence usually beats natural stupidity."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert uint16_t to std::string?
    By magicalo in forum C++ Programming
    Replies: 3
    Last Post: 07-18-2008, 01:34 AM
  2. What is the easies way to convert int to std::string?
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 12-04-2007, 02:15 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. How to convert a string to a character array?
    By Aven in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2005, 11:14 PM
  5. How do i convert a string to a character array?
    By Susan in forum C++ Programming
    Replies: 8
    Last Post: 01-01-2002, 02:31 PM