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)
Convert string to Uppercase string in C Using toupper() Function.
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.
toupper() works only on a single character. But there is strupr() which is what you want for a pointer to a string.
The strtoupper() function converts a string to uppercase.
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.
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.
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.
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.
You need to put a double colon before toupper
:
transform(input.begin(), input.end(), input.begin(), ::toupper);
Explanation:
There are two different toupper
functions:
toupper
in the global namespace (accessed with ::toupper
), which comes from C.
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.
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 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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With