I'm not sure what you mean exactly by "using char instead of string," but if that means something like
for (int i = 0; i < s.size(); ++i) {
s[i] = toupper(s[i]);
}
then you should be fine.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string myString ("abcd");
transform(myString.begin(),myString.end(),myString.begin(),toupper);
cout << myString << endl;
cin.ignore();
return 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>transform(myString.begin(),myString.end(),myString.begin(),toupper);
Amazingly enough, that's correct, but I'm not sure if it's correct because you intended it to be or if it's correct because you messed up your headers. I don't recommend using toupper directly with transform because most programs will want to use the toupper declared in , but will also include , which declares another form of toupper. This causes an ambiguity and the code will mysteriously fail to work. The best fix is to write a wrapper around the toupper that you want:
struct upper {
int operator() ( int c )
{
return toupper ( static_cast<unsigned char> ( c ) );
}
};
transform(myString.begin(),myString.end(),myString.begin(),upper());
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
u can use
for (i=0;s[i]!='\0';i++)
s[i]=toupper(s[i]);
that may not work because std::string is NOT a null-terminated string. It does not use 0 to determine end of string.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>transform(myString.begin(),myString.end(),myString.begin(),toupper);
Amazingly enough, that's correct, but I'm not sure if it's correct because you intended it to be or if it's correct because you messed up your headers.
some compilers may have problems with the code I posted for the reason you mentioned (ambiguity). The code you posted is indeed more portable between compilers.
I use Microsoft compiler(s) and the code I originally posted works with them.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
some compilers may have problems with the code I posted for the reason you mentioned (ambiguity). The code you posted is indeed more portable between compilers.
I use Microsoft compiler(s) and the code I originally posted works with them.
Hello anicent dragron, the code you posted doesn't work on my compiler. How do I make it work? :sad:
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Hello anicent dragron, the code you posted doesn't work on my compiler. How do I make it work? :sad:
see Narue's post.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343