C++ — Convert string to upper/lower case

by

To convert an std::string to upper case you can use the following:

#include <algorithm>
#include <string>

std::string data = “Abc”;
std::transform(data.begin(), data.end(),
data.begin(), ::toupper);

For converting to lower case, you need to replace ::toupper with ::tolower.

Other C/C++ not so frequently asked questions.

About these ads

5 Responses to “C++ — Convert string to upper/lower case”

  1. top selling guitar Says:

    top selling guitar…

    [...]C++ — Convert string to upper/lower case « Not So Frequently Asked Questions[...]…

  2. les paul Says:

    les paul…

    [...]C++ — Convert string to upper/lower case « Not So Frequently Asked Questions[...]…

  3. STL String to lower case | Everyday I'm coding Says:

    [...] From http://notfaq.wordpress.com/2007/08/04/cc-convert-string-to-upperlower-case/: [...]

  4. Zenju Says:

    tolower takes an int; std::string contains potentially unsigned chars, in which case the code above would be buggy.
    => missing static_cast!

    • likern Says:

      Why it’s buggy?
      Assigning unsigned value to signed is undefined behaviour only if
      unsigned is out of range signed value – bigger. But unsigned char is maximum 255 value which is obviously smaller than signed int – at least 16 bit.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: