reverse string upper to lower
To reverse a string from upper case to lower case in C++, you can follow these steps:
- Start by including the necessary header file for string manipulation:
#include <string>
- Declare a string variable to store the input string:
std::string inputString;
- Prompt the user to enter the string and store it in the variable:
std::cout << "Enter a string: ";
std::getline(std::cin, inputString);
- Iterate over each character in the string using a loop:
for (int i = 0; i < inputString.length(); i++) {
// Reverse the case of each character
}
- Inside the loop, check if the current character is an uppercase letter using the
isupper()
function from the<cctype>
header:
if (std::isupper(inputString[i])) {
// Convert the character to lowercase
}
- If the current character is an uppercase letter, convert it to lowercase using the
tolower()
function from the<cctype>
header:
inputString[i] = std::tolower(inputString[i]);
- Finally, after the loop, output the reversed string:
std::cout << "Reversed string: " << inputString << std::endl;
Here is the complete code snippet:
#include <iostream>
#include <string>
#include <cctype>
int main() {
std::string inputString;
std::cout << "Enter a string: ";
std::getline(std::cin, inputString);
for (int i = 0; i < inputString.length(); i++) {
if (std::isupper(inputString[i])) {
inputString[i] = std::tolower(inputString[i]);
}
}
std::cout << "Reversed string: " << inputString << std::endl;
return 0;
}
This code prompts the user to enter a string, then iterates over each character in the string. If a character is an uppercase letter, it converts it to lowercase using the tolower()
function. Finally, it outputs the reversed string with all uppercase letters converted to lowercase.