C Program#155 – Convert Upper Case to Lower Case & vice versa (Reverse Case).

Program to Convert Upper Case to Lower Case & vice versa (Reverse Case) (Conditional Operator):

Program Logic : Convert Upper Case to Lower Case & vice versa (Reverse Case)

  • Upper Case (A-Z) have ASCII Codes 65 to 90 in ASCII Table.
  • Lower Case (a-z) have ASCII Codes 97 to 122 in ASCII Table.
  • Difference between their ASCII Codes is 32 in ASCII Table.
  • For Upper to Lower Case add 32 as ch=ch+32.
  • For Lower to Upper Case subtract 32 as ch=ch-32.
  • Declare a single character variable, take input of single character using %c in scanf() function.
  • First, find out character is Upper Case or Lower Case by Nesting Conditional Operator such as result=(ch>=65 && ch<=90)?1:(ch>=97 && ch<=122)?2:0 .
  • Then, convert using ch=(result==1)?ch+32:(result==2)?ch-32:ch;.
  • Still not understood ? See the proper working example in action below.




Full Well Commented, Self-Explanatory Example Program of the same given below.

Below Here are Four (4) different Outputs of the program.



Output of the Program (Run 1 – Input: UPPER CASE):

Output of the Program (Run 2 – Input – LOWER CASE):



Output of the Program (Run 3 – Input – DIGIT):

Output of the Program (Run 4 – Input – SPECIAL CHARACTER):