Convert Lowercase to Uppercase and Vice Versa
“convert lowercase to uppercase and vice versa” is a basic problem of string data structure. Here, we are given a string of length ‘n’ and our task is to convert upper-case alphabets to lower-case and vice-versa.
String might contain characters except alphabets too, in that case, we will ignore them.
Example:
INPUT: HelpMeStudyBro OUTPUT: hELPmEsTUDYbRO
The steps to required to convert lowercase to uppercase and vice versa are as follows:
- Scan the complete string.
- If the character of string is upper-case alphabet, add 32 with it.
- If the character of string is lower-case alphabet, subtract 32 from it.
C++ Program to convert lowercase to uppercase and vice versa is as follows:
/* Program to convert lowercase to uppercase and vice versa */ #include<bits/stdc++.h> using namespace std; int main() { string str; cout<<"Enter a string:\n"; cin>>str; int size = str.length(); for(int i = 0; i < size; i++) { if(str[i]>='A' && str[i]<='Z') { str[i] = str[i] + 32; } else if(str[i]>='a' && str[i]<='z') { str[i] = str[i] - 32; } } cout<<"\nString after converting cases of letters are:\n"<<str; }
OUTPUT: Enter a string: HelpMeStudyBro String after converting cases of letters are: hELPmEsTUDYbRO Time Complexity: O(n), where n is the length of string.