C++ Exercises: Change the case of each character of a given string
C++ String: Exercise-13 with Solution
Write a C++ program to change the case (lower to upper and upper to lower cases) of each character of a given string.
Pictorial Presentation:
Sample Solution:
C++ Code :
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string change_Case(string text) {
for (int x = 0; x < text.length(); x++)
{
if (isupper(text[x]))
{
text[x] = tolower(text[x]);
}
else
{
text[x] = toupper(text[x]);
}
}
return text;
}
int main() {
cout << "Original string: Python, After changing cases-> "<< change_Case("Python") << endl;
cout << "Original string: w3resource, After changing cases-> "<< change_Case("w3resource") << endl;
cout << "Original string: AbcdEFH Bkiuer, After changing cases-> "<< change_Case(" AbcdEFH Bkiuer") << endl;
return 0;
}
Sample Output:
Original string: Python, After changing cases-> pYTHON Original string: w3resource, After changing cases-> W3RESOURCE Original string: AbcdEFH Bkiuer, After changing cases-> aBCDefh bKIUER
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C++ program to insert a dash character (-) between two odd numbers in a given string of numbers.
Next:Write a C++ program to find the numbers in a given string and calculate the sum of all numbers.
What is the difficulty level of this exercise?