Convert uppercase to lowercase and vice versa

C program to convert uppercase letter to lowercase and vice versa.

Program

#include<stdio.h>
#include<conio.h>
void main()
{
	char ch;

    printf("Enter the character\t:");
    scanf("%c",&ch);

    if((ch >= 'a') && (ch <= 'z'))
    {
        ch = ch - 32;
        printf("\nCorresponding uppercase character is\t:%c",ch);
    }
    else if((ch >= 'A') && (ch <= 'Z'))
    {
        ch = ch + 32;
        printf("\nCorresponding lowercase character is\t:%c",ch);
    }
    else
    {
		printf("\nInput is not a character");
    }

    getch();
}

Output

********** Run1 **********

Enter the character     :R

Corresponding lowercase character is    :r


********** Run1 **********

Enter the character     :h

Corresponding uppercase character is    :H

Explanation

In the above program, if user enters an uppercase letter, then it will be converted to lowercase. And if user enters an lowercase letter, then it will be converted to uppercase letter.
This is done using the ASCII values. The difference in ASCII values between the lowercase letters and uppercase letters is 32.
Thus, if letter is lowercase then we will subtract 32 to obtain its corresponding uppercase character.
ch=ch–32;
In the above expression, ch is char and 32 is int, we are subtracting integer value from character value, But this is not the case. C compiler will take the ASCII value of the letter and then subtract 32 from it.
And, if letter is uppercase then we will add 32 to obtain its corresponding lowercase character.
ch=ch+32;