C Program to Check Whether a Character is Lowercase or not

English alphabets are made up of 26 letters. And these alphabets are lowercase i.e. a, b, c, d, ..., z and uppercase A, B, C, ..., Z.

In this C program, we are going to check whether a given character by user is lowercase or not. And for this we need to understand concept of ASCII value. ASCII is abbreviated form for American Standard Code for Information Interchange. Since computers can only understand numbers, so an ASCII code or value is Numeric representation of character such as 'B' or 'b' or '%' etc.

To check whether a character is lowercase or not we need to check whether ASCII value of given character falls in the range of lowercase character or not. Since ASCII value for lowercase alphabet ranges from 97 to 122.

Since character data type, behind the scene, stores number i.e. corresponding ASCII value. So we can compare directly with number.

Program


#include<stdio.h>
#include<conio.h>

int main()
{
	 char ch;
	 clrscr();
	 printf("Enter any character: ");
	 scanf("%c", &ch);
	
	 if(ch>=97 && ch<=122)
	 {
	  	printf("%c is LOWERCASE.", ch);
	 }
	 else
	 {
	  	printf("%c is NOT LOWERCASE.", ch);
	 }
	 getch();
	 
	 return(0);
}

Output of above program :

Run 1:
----------
Enter any character: f ↲
f is LOWERCASE.

Run 2:
----------
Enter any character: G ↲
G is NOT LOWERCASE.

Run 3:
----------
Enter any character: * ↲
* is NOT LOWERCASE.

Note: ↲ indicates ENTER is pressed.

Explanation (Checking Lowercase or Not)

In the above program, first character variable ch is declared. And then given character by user is stored in ch. After reading character ch it is checked whether it is within range of 97 (ASCII value of lowercase character a) and 122 (ASCII value of lowercase character z) or not by using if(ch>=97 && ch<=122)

But! Why to remember ASCII Value?

Consider following case:

char ch;
ch = 'A';	

What is actually stored in variable ch?

We can find it by printing ch i.e.

printf("%c", ch);

Output of the above statement will be:

A 

BUT!, if we print value of variable ch in decimal format i.e.

printf("%d", ch);

Then output of the above statement will be:

65

So, what happened here? What is this value? Now you might have guessed that it is ASCII value of character A so behind the scene when we write 'A' it is equivalent to writing 65.

So, in the above program i.e. to check whether a given character is lowercase or not, condition if(ch>=97 && ch<=122) can be replaced by condition if(ch>='a' && ch<='z') and in this program you don't need to remember ASCII values!!

Modified Program


#include<stdio.h>
#include<conio.h>

int main()
{
	 char ch;
	 clrscr();
	 printf("Enter any character: ");
	 scanf("%c", &ch);
	
	 if(ch>='a' && ch<='z')
	 {
	  	printf("%c is LOWERCASE.", ch);
	 }
	 else
	 {
	  	printf("%c is NOT LOWERCASE.", ch);
	 }
	 getch();
	 return(0);
}

Output

Run 1:
----------
Enter any character: f ↲
f is LOWERCASE.

Run 2:
----------
Enter any character: G ↲
G is NOT LOWERCASE.

Run 3:
----------
Enter any character: * ↲
* is NOT LOWERCASE.

Note: ↲ indicates ENTER is pressed.