convering string toupper case ..

Hello All ,

How could we convert 'string' type to its upper case ?
In normal , we know the 'char' could be converted easily . But what is about the string ?!


I am waiting your replies as soon as possible ..


Regards ..
You can loop through the characters in the string and use std::toupper to convert each of them to upper case.
for each character in string
character = touppercase(character)

print string
1
2
3
4
5
		std::string s( "This is a test" );

		std::transform( s.begin(), s.end(), s.begin(), std::toupper );

		std::cout << "s = \"" << s << "\"\n";
@vlad, There are two versions of std::tolower. One in <cctype> and another in <locale>. If both happen to be declared your code will fail to compile.
Last edited on
Are you sure that the code will not be compiled?! Inside the transform a function with one parameter is called. At least MS VC++ 2010 compiles it without problem.
In any case you can write


1
2
std::transform( s.begin(), s.end(), s.begin(), 
                       [] ( char c ) { return ( std::toupper( c ) ); } );
@vlad, for this code
1
2
std::transform( s.begin(), s.end(), s.begin(), 
                       [] ( char c ) { return ( std::toupper( c ) ); } );


MS VC++ 2010 Ultimate gives:
error C2039: 'transform' : is not a member of 'std'
error C2039: 'toupper' : is not a member of 'std'
error C3861: 'transform': identifier not found
error C2039: 'toupper' : is not a member of 'std'


-----------------------------------------
EDITED:
Works fine and without any errors, but must be like this:
1
2
3
#include <algorithm>
std::transform( s.begin(), s.end(), s.begin(), 
                       [] ( char c ) { return ( toupper( c ) ); } );
Last edited on
Topic archived. No new replies allowed.