C Program To Check Whether a Character is an Alphabet or Not


Lets write a C program to check if user input character is an alphabet or not.

Related Read:
Relational Operators In C
Logical Operators In C

Note: In C programming language, every character variable holds an ASCII value rather than the character itself. You can check ASCII value of all the characters here: C Program To Print All ASCII Characters and Code

Also watch video tutorial: C Program to Print ASCII Value of a Character

Note: ASCII value range of lower case alphabets:
ASCII value of a is 97.
ASCII value of b is 98.
ASCII value of c is 99.
and so on till z ..
ASCII value of z is 122.

ASCII value range of upper case alphabets:
ASCII value of A is 65.
ASCII value of B is 66.
ASCII value of C is 67.
and so on till z ..
ASCII value of Z is 90.

Source Code: C Program To Check Whether a Character is an Alphabet or Not

  1. #include < stdio.h >  
  2.   
  3. int main()  
  4. {  
  5.     char c;  
  6.   
  7.     printf("Enter a character\n");  
  8.     scanf("%c", &c);  
  9.   
  10.     if( (c >= 'a' && c <= 'z') ||  
  11.         (c >= 'A' && c <= 'Z') )  
  12.     {  
  13.         printf("%c is an alphabet\n", c);  
  14.     }  
  15.     else  
  16.     {  
  17.         printf("%c is not an alphabet\n", c);  
  18.     }  
  19.   
  20.     return 0;  
  21. }  

Output 1:
Enter a character
5
5 is not an alphabet

Output 2:
Enter a character
a
a is an alphabet

Output 3:
Enter a character
$
$ is not an alphabet

Output 4:
Enter a character
S
S is an alphabet

Output 5:
Enter a character
#
# is not an alphabet

Logic To Check Whether a Character is an Alphabet or Not

We accept the character from user as input. We check if the user entered character is greater than or equal to a and less than or equal to z OR the user entered character is greater than or equal to A and less than or equal to Z. If these conditions are met, then user entered character is an alphabet or else its not.

Video Tutorial: C Program To Check Whether a Character is an Alphabet or Not



YouTube Link: https://www.youtube.com/watch?v=uY3d6SXBBNE [Watch the Video In Full Screen.]

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *