954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Changing strin into uppercase

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

muthuivs
Light Poster
32 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

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?

muthuivs
Light Poster
32 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

u can use

[code]
for (i=0;s[i]!='\0';i++)
s[i]=toupper(s[i]);

beuls
Light Poster
40 posts since Jan 2006
Reputation Points: 13
Solved Threads: 2
 
#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
Team Colleague
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
Administrator
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
Team Colleague
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
Team Colleague
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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

muthuivs
Light Poster
32 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 
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!

beuls
Light Poster
40 posts since Jan 2006
Reputation Points: 13
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You