39

I need to convert a string in C++ to full upper case. I've been searching for a while and found one way to do it:

#include <iostream> #include <algorithm> #include <string> using namespace std; int main() { string input; cin >> input; transform(input.begin(), input.end(), input.begin(), toupper); cout << input; return 0; } 

Unfortunately this did not work and I received this error message:

no matching function for call to 'transform(std::basic_string::iterator, std::basic_string::iterator, std::basic_string::iterator,

I've tried other methods that also did not work. This was the closest to working.

So what I'm asking is what I am doing wrong. Maybe my syntax is bad or I need to include something. I am not sure.

I got most of my info here: http://www.cplusplus.com/forum/beginner/75634/ (last two posts)

1

7 Answers 7

60

You need to put a double colon before toupper:

transform(input.begin(), input.end(), input.begin(), ::toupper); 

Explanation:

There are two different toupper functions:

  1. toupper in the global namespace (accessed with ::toupper), which comes from C.

  2. toupper in the std namespace (accessed with std::toupper) which has multiple overloads and thus cannot be simply referenced with a name only. You have to explicitly cast it to a specific function signature in order to be referenced, but the code for getting a function pointer looks ugly: static_cast<int (*)(int)>(&std::toupper)

Since you're using namespace std, when writing toupper, 2. hides 1. and is thus chosen, according to name resolution rules.

Sign up to request clarification or add additional context in comments.

5 Comments

@LokiAstari: The point is that you want the global toupper, not the std:: one(s), which highlights the problem with use namespace std -- it pollutes the default namespace with lots of symbols you don't want just to get one or two. If you want to import just a few symbols, you should import just those symbols, not the entire std namespace.
@LokiAstari: No -- if you remove the using namespace std; then you can use just toupper in this code and it will work (no need for the ::). Of course, you need to add using std::transform; (or stick std:: before transform) and similarly for other symbols in std that are being used.
In addition, you may need #include <ctype.h> instead of #include <cctype> to get toupper in the global namespace -- but that's independent of everything else. (<cctype> is allowed to put toupper in the global namespace, but not required to)
@leemes How can I do if I want to only first character in string to uppercase? I tried with ::toupper(input[0]) but it didn't work.
@HasanBasri toupper returns the modified character instead of updating it in place. That means, you need to assign back the result: input[0] = toupper(input[0]). (Here, I didn't put the two colons, which effectively means I want std::toupper when using namespace std).
10

Boost string algorithms:

#include <boost/algorithm/string.hpp> #include <string> std::string str = "Hello World"; boost::to_upper(str); std::string newstr = boost::to_upper_copy("Hello World"); 

Convert a String In C++ To Upper Case

Comments

7

Try this small program, straight from C++ reference

#include <iostream> #include <algorithm> #include <string> #include <functional> #include <cctype> using namespace std; int main() { string s; cin >> s; std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(std::toupper)); cout << s; return 0; } 

Live demo

1 Comment

std::ptr_fun() is deprecated in c++11 and removed in c++14.
6

Uppercase to Lowercase and viceversa using BitWise operators

1.

string s = "cAPsLock"; for(char &c: s) c = c | ' '; // similar to: c = tolower(c); cout << s << endl; // output: capslock 
string s = "cAPsLock"; for(char &c: s) c = c & ~' '; // similar to: c = toupper(c); cout << s << endl; // output: CAPSLOCK 

PS: for more info check this link

Comments

3

You could do:

string name = "john doe"; //or just get string from user... for(int i = 0; i < name.size(); i++) { name.at(i) = toupper(name.at(i)); } 

Comments

3
#include <iostream> using namespace std; //function for converting string to upper string stringToUpper(string oString){ for(int i = 0; i < oString.length(); i++){ oString[i] = toupper(oString[i]); } return oString; } int main() { //use the function to convert string. No additional variables needed. cout << stringToUpper("Hello world!") << endl; return 0; } 

Comments

0

Like leemes said, you can use toupper(int). Like this:

void ToUpper(string &str) { for (auto beg = str.begin(); beg != str.end(); ++beg) { *beg = toupper(*beg); } } 

It'll through in each character from str and convert it to upper. Example:

int main() { string name; cout << "Insert a name: "; cin >> name; ToUpper(name); cout << "Name in upper case: " << name << endl; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.