Change case of string input – CSKC
Sign In

Search

Solved Problem #CPP#2625    siteicon   siteicon  

Problem Statement - Change case of string input

Write a program to get input of a string from the user as a character array and then print the output with changed case from lower to upper and upper to lower for every character present in it. Do not taken any action if the character is not an alphabet, just print as it is.

Solution

TC++ #2625

 

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

  Related ( ? ) :

char str[80]; cout<<“Input a string:” ; cin.getline(str,80);
Collecting user given string in char array str.

for(int i=0;str[i]!=”;i++) //Traversing string till the null is found
{
if(str[i]>=’a’ && str[i]<=’z’ ) //Finding if the character as picked is within lower case range.
cout<<char (str[i]-32);  //lower to upper is converted by subtracting 32 and converting to character again by type casting.
else if(str[i]>=’A’ && str[i]<=’Z’) //Finding if the character as picked is within upper case range.
cout<<char (str[i]+32);//Upper to is converted by adding 32 and converting to character again by type
else cout<<str[i]; //Any other character is printed as it is
}

Notes

  • Such program may come in different variations of change case of first character or making all string as upper and all string as lower.
  • The popular DOS based compiler versions also give functions like toupper() and tolower(). Such functions are non-universal and are discouraged in modern programming practices.

Common Errors

  • Student may miss printing other characters while solving such problems.


Share

Created: 13-Jan-2018 22:55 | Updated: 13-Jan-2018 23:04|