A character is said to be in uppercase if it is in capital letter and it is said to be in lowercase if it is in small letter. C program to check whether an entered character is in uppercase of lowercase is shown below.
Example 1: Program to Check Uppercase / Lowercase Using ASCII value
#include<stdio.h>
int main()
{
char c;
printf ("Enter a character n");
scanf ("%c", &c);
if (c>64 && c<91)
{
printf ("It is uppercase character");
}
else
{
printf ("It is in lowercase character");
}
return 0;
}
We know that the ASCII value of lowercase alphabet ‘a’ is 97, ‘b’ is 98 … ‘z’ is 122. And the ASCII value of uppercase alphabet ‘A’ is 65, ‘B’ is 66 … ‘Z’ is 90. So the program displays whether the entered character is lowercase alphabet or uppercase alphabet by checking its ASCII value.
Another method to check in C program whether entered character is in lowercase or uppercase is by comparing the entered character with the alphabets itself. This is shown below.
Example 2: Program to Check Uppercase / Lowercase Using Character Comparison
#include<stdio.h>
int main()
{
char c;
printf ("Enter a character n");
scanf ("%c", &c);
if (c>='A' && c<='Z')
{
printf ("It is uppercase character");
}
else
{
printf ("It is lowercase character");
}
return 0;
}
Here, instead of remembering the ASCII value of ‘a’ or ‘A’ and ‘z’ or ‘Z’, we directly compare the entered character as c>=’A’ and c<=’Z’.
Example 3: Program to Check Uppercase / Lowercase Using Library Function isupper() and islower()
#include<stdio.h>
#include<ctype.h>
int main()
{
char c;
printf ("Enter a character n");
scanf ("%c", &c);
if (isupper(c))
{
printf ("It is UPPERCASE character");
}
else if (islower(c))
{
printf ("It is lowercase character");
}
else
{
printf("It is not a character");
}
return 0;
}
The isupper() function returns 1 if the parameter passed is uppercase character and 0 if parameter passed is lowercase character.
The islower() function returns 1 if the parameter passed is lowercase character and 0 if parameter passed is uppercase character.
Output
Enter a character H It is in uppercase
Enter a character h It is in lowercase.