Write a program that prompts the user to input a string, then convert each lowercase character in it to uppercase,
and all uppercase character to lowercase. You must use character array in implementing your solution.
Sample Run:
Enter a string: Computer PROGRAMMING is Fun!
cOMPUTER programming IS fUN!
No, it’s not!
#include <iostream>
using namespace std;
void convertOpposite(string & str)
{
int ln=str.length();
for(int i=0;i<ln;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;
}}
int main(){
string str;
cout<<"Enter the string: "<<endl;
cin>>str;
convertOpposite(str);
cout<<str;
return 0;
}
Comments
Leave a comment