2010年11月25日 星期四

C++ - how to convert string to uppercase/lowercase

C++ - how to convert string to uppercase/lowercase:
On Dec 17, 12:29 pm, Michal wrote:
> Hallo
> I looked through ANSI/ISO C++ standard string, and I did not find any
> function from string class that would do so. Did I overlooked
> something or it is so?


Here's the STL-ish way.

//////
#include
#include
#include

int my_toupper(int c)
{
return toupper(c);
}

int
main(int argc, char **argv)
{
using namespace std;
string s = "hello world";
transform(s.begin(), s.end(), s.begin(), my_toupper);
}
////

You have to create my_toupper (or use a cast) due to a
C++ wart.

Sean