convering string toupper case ..

Apr 28, 2012 at 1:48pm
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 ..
Apr 28, 2012 at 1:57pm
You can loop through the characters in the string and use std::toupper to convert each of them to upper case.
Apr 28, 2012 at 2:27pm
for each character in string
character = touppercase(character)

print string
Apr 28, 2012 at 3:37pm
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";
Apr 28, 2012 at 4:00pm
@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 Apr 28, 2012 at 4:01pm
Apr 28, 2012 at 4:22pm
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 ) ); } );
May 7, 2012 at 2:13pm
@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 May 7, 2012 at 3:42pm
Topic archived. No new replies allowed.