convert whole string to uppercase c++
To convert a whole string to uppercase in C++, you can use the toupper()
function from the <cctype>
library. Here are the steps to do so:
- Include the
<cctype>
library at the beginning of your program by adding the following line of code:
#include <cctype>
- Declare a string variable to store the input string and a string variable to store the uppercase version of the string. For example:
#include <iostream>
#include <cctype>
#include <string>
int main() {
std::string inputString;
std::string uppercaseString;
// Rest of the code...
}
- Read the input string from the user using
std::getline()
or any other appropriate method. For example:
std::cout << "Enter a string: ";
std::getline(std::cin, inputString);
- Iterate over each character in the input string and convert it to uppercase using the
toupper()
function. Append the converted character to the uppercase string. For example:
for (char c : inputString) {
uppercaseString += std::toupper(c);
}
- Print the uppercase string. For example:
std::cout << "Uppercase string: " << uppercaseString << std::endl;
Putting it all together, here's an example of a C++ program that converts a string to uppercase:
#include <iostream>
#include <cctype>
#include <string>
int main() {
std::string inputString;
std::string uppercaseString;
std::cout << "Enter a string: ";
std::getline(std::cin, inputString);
for (char c : inputString) {
uppercaseString += std::toupper(c);
}
std::cout << "Uppercase string: " << uppercaseString << std::endl;
return 0;
}
This program asks the user to enter a string, converts each character to uppercase, and then prints the uppercase version of the string.