I need to change all the chancters from a string to uppercase. the toupper() function only works for charachters but not strings. My program looks like this:

int main()
{

string myString;

cout<<"Enter the String : ";
cin>>myString;
myString = toupper(myString); //this doesnt work, i guess since its not char.
}

Please help. Thanks.


MuthuIVS

Recommended Answers

All 11 Replies

I got it to work but I had to use a for loop by first using char instead of string then going through each char value and using the toupper function. Is this the best way?

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.

u can use

for (i=0;s[i]!='\0';i++)
 s[i]=toupper(s[i]);
#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;
}

>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 <cctype>, but will also include <iostream>, 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());

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.

>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.

Member Avatar for iamthwee

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:

Hello anicent dragron, the code you posted doesn't work on my compiler. How do I make it work? :sad:

see Narue's post.

i gpt it to work using a loop but the string conversion code posted by Acient Dragon works too....Very intersting code thouh..Should use it more often unless we are planning to use multiple compilers. Thanks.

Muthu

that may not work because std::string is NOT a null-terminated string. It does not use 0 to determine end of string.

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.