How to Convert a C++ String to Uppercase

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


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


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.