Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert a C++ String to Uppercase

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)

like image 325
Thomas W. Avatar asked May 01 '14 23:05

Thomas W.


People also ask

Which c string function converts a string to uppercase?

Convert string to Uppercase string in C Using toupper() Function.

How do you convert a string to uppercase?

Java String toUpperCase() Method The toUpperCase() method converts a string to upper case letters. Note: The toLowerCase() method converts a string to lower case letters.

Does toupper work on strings in c?

toupper() works only on a single character. But there is strupr() which is what you want for a pointer to a string.

Which function converts a string to all uppercase?

The strtoupper() function converts a string to uppercase.

How to convert character to uppercase in C program?

How to write a C Program to convert character to uppercase using the built-in function toupper, and also by not using toupper function. The C Programming toupper is a built-in function present in the <ctype.h> header file.

How to convert string to lowercase in C language?

Here is the program to convert a string to lowercase in C language, In the above program, the actual code of conversion of string to lowercase is present in main ()function. An array of char type s [100] is declared which will store the entered string by the user.

How to convert a string to uppercase in Python?

We can achieve this in multiple ways, but we will discuss four different approaches: using For Loop, While Loop, Functions, and ASCII Values This program allows the user to enter any string or character array. Next, it will use For Loop to iterate every character in that string, and convert them to Uppercase.

How to convert a string to upper case using for loop?

2) Check the each ASCII value of the string is in the range between 97 to 122 (lowercase alphabets range).If the character is in lower case then subtract 32 from its ASCII value, then the character will be converted into upper case. 3) After all iterations of for loop print the string which is in upper case.


4 Answers

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.

like image 188
leemes Avatar answered Oct 08 '22 21:10

leemes


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

like image 39
Michael S Bergin Avatar answered Oct 08 '22 20:10

Michael S Bergin


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

like image 7
yizzlez Avatar answered Oct 08 '22 21:10

yizzlez


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));
}
like image 3
jordpw Avatar answered Oct 08 '22 21:10

jordpw