C++ Functions to Convert String to Upper Case
C++ provides different functions to convert a string to uppercase. The most used functions are:
Convert String to Uppercase Using toupper() Function
In C++, toupper() is a standard library function declared in the <cctype> header file, which converts a given lowercase alphabet character to its corresponding uppercase character. Here’s the syntax of toupper() function:
where c is the character to be converted to uppercase. The function returns the uppercase equivalent of c if it is a lowercase alphabet character; otherwise, it returns c unchanged. To convert all the characters into a string, you need to apply this function to each character in the string.
Here’s an example:
using namespace std;
int main() {
string str = "hello world";
for(int i = 0; i < str.length(); i++) {
str[i] = toupper(str[I]);
}
cout << str << endl;
return 0;
}
This code declares a string variable named “str” and initializes it with the value “hello world”. Next a for loop is defined that will iterate through each character of the string using the index variable “i”.
Inside the loop, the toupper() function is called on each character to convert it to uppercase. The loop ends when the index variable “i” reaches the length of the string. Finally, the modified string is printed to the console using cout. The program then returns 0, indicating successful execution
Output
Convert String to Upper Case Using transform() Function
Another built-in C++ function that can convert string characters to uppercase is transform() function. It is more efficient than toupper() since it converts the entire string in one go. Here’s an example:
#include <algorithm>
using namespace std;
int main() {
string str = "hello world";
transform(str.begin(), str.end(), str.begin(), ::toupper);
cout << str << endl;
return 0;
}
This code declares a string variable named “str” and initializes it with the value “hello world”. It then uses the transform() function from the algorithm library to convert the entire string to uppercase letters.
The transform() function takes three arguments: the beginning and end of the string characters to transform, and a third argument specifies the transformation operation.
In the above program the third argument is the toupper() function that converts each character to uppercase. The “::toupper” specifies that we are using the toupper() function from the global namespace. Finally, the modified string is printed to the console using cout. The program then returns 0, indicating successful execution.
Output
Convert User Input String to Upper Case
Now we will write a program that takes string input from the user and converts it to upper case.
#include <string>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
for (int i = 0; i < str.length(); i++) {
str[i] = toupper(str[I]);
}
cout << "String in upper case: " << str << endl;
return 0;
}
This code uses the getline() function to take user input as a string, and then loops through each character in the string and applies the toupper() function to convert it to upper case. Finally, it prints the resulting string in upper case to the console.
Output
Conclusion
In this article, we explored different ways of converting a string to uppercase in C++. You can use built-in functions like toupper() and transform() to convert any of the string to Upper Case. For a complete guide to these two functions, read the above instructions.