Convert String to Uppercase in C++
Converting a string to uppercase is a common requirement in programming, and it can be easily achieved in C++. In this blog post, we will discuss how to convert a string to uppercase in C++ with syntax, example code, and output.
Syntax:
We can use the std::toupper() function to convert a string to uppercase in C++. The syntax of the function is as follows:
#include <cctype>
int toupper(int c);
Here, c is the character that we want to convert to uppercase. The function returns the uppercase equivalent of c.
Example Code:
Now, let's take a look at an example code to understand how to use the std::toupper() function to convert a string to uppercase in C++. In this example, we will read a string from the user and convert it to uppercase.
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
string str;
cout<< "Enter a string: ";
getline(cin, str);
// Convert string to uppercase
for (int i = 0; i<str.length(); i++)
{
str[i] = toupper(str[i]);
}
cout<< "Uppercase string: " << str <<endl;
return 0;
}
Output:
Let's run the above code and see what output we get when we enter a string.
Enter a string: hello world
Uppercase string: HELLO WORLD
Explanation:
In the above code, we first declare a string variable str. After that, we prompt the user to enter a string using the cout and getline() functions.
Next, we use a for loop to iterate over each character in the string. Inside the loop, we use the toupper() function to convert each character to uppercase. We assign the uppercase character back to the same index in the string using the assignment operator.
Finally, we print the uppercase string using the cout function.
There are other ways to convert a string to uppercase in C++, besides using the std::toupper() function. Let's take a look at a few other methods.
Method 1: Using transform() function
The std::transform() function can be used to convert a string to uppercase in C++. The std::transform() function applies a given function to each element in the input range, and stores the result in the corresponding element in the output range.
Syntax:
The syntax of the std::transform() function is as follows:
template <class InputIterator, class OutputIterator, class UnaryOperation>
OutputIterator transform (InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperation op);
Here, first1 and last1 define the input range, and result is the output range. The op parameter is a unary function that will be applied to each element in the input range.
Example:
Let's take a look at an example code that uses the std::transform() function to convert a string to uppercase:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str;
cout<< "Enter a string: ";
getline(cin, str);
// Convert string to uppercase
transform(str.begin(), str.end(), str.begin(), ::toupper);
cout<< "Uppercase string: " << str <<endl;
return 0;
}
Output:
Enter a string: Hello World!
Uppercase string: HELLO WORLD!
Explanation:
In the above code, we use the transform() function to convert each character in the string to uppercase. The first two parameters of the transform() function specify the range of characters that we want to transform, which is the entire string in this case. The third parameter is the output range, which is the same as the input range in this case. The fourth parameter is the function that we want to apply to each character, which is the toupper() function.
Conclusion:
In conclusion, converting a string to uppercase in C++ is a simple task that can be achieved using the std::toupper() function. We can use a for loop to iterate over each character in the string and convert it to uppercase using the toupper() function. We hope that this blog post has helped you understand how to convert a string to uppercase in C++.