C Program To Check For Alphabet, Number and Special Symbol


Any character is entered through the keyboard, write a C program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol.

The following table shows the range of ASCII values for various characters:
Character A – Z : ASCII Value 65 – 90
Character a – z : ASCII Value 97 – 122
Character 0 – 9 : ASCII Value 48 – 57
Special Symbol : ASCII Value 0 – 47, 58 – 64, 91 – 96, 123 – 127

ascii codes

Related Read:
else if statement in C
Relational Operators In C
C Program To Print All ASCII Characters and Code

Expected Output for the Input

User Input:
Enter a Character
$

Output:
$ is a Special Character

Video Tutorial: C Program To Check For Alphabet, Number or Special Symbol


[youtube https://www.youtube.com/watch?v=NBcG-r0P9P8]

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

Source Code: C Program To Check For Alphabet, Number and Special Symbol

  1. #include<stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     char ch;  
  6.   
  7.     printf("Enter a Character\n");  
  8.     scanf("%c", &ch);  
  9.   
  10.     if(ch >= 65 && ch <= 90)  
  11.     {  
  12.         printf("%c is an Uppercase Alphabet\n", ch);  
  13.     }  
  14.     else if(ch >= 97 && ch <= 122)  
  15.     {  
  16.         printf("%c is an lowercase Alphabet\n", ch);  
  17.     }  
  18.     else if(ch >= 48 && ch <= 57)  
  19.     {  
  20.         printf("%c is a Number\n", ch);  
  21.     }  
  22.     else if( (ch >= 0  && ch <= 47) ||  
  23.              (ch >= 58 && ch <= 64) ||  
  24.              (ch >= 91 && ch <= 96) ||  
  25.              (ch >= 123 && ch <= 127))  
  26.     {  
  27.         printf("%c is a Special Character\n", ch);  
  28.     }  
  29.   
  30.     return 0;  
  31. }  

Output 1:
Enter a Character
A
A is an Uppercase Alphabet

Output 2:
Enter a Character
i
i is an lowercase Alphabet

Output 3:
Enter a Character
8
8 is a Number

Output 4:
Enter a Character
$
$ is a Special Character

Logic To Check For Alphabet, Number and Special Symbol

We use &&(AND) operator check check for range. i.e., for number, we check from the range 48 to 57. To check if the user entered character lies in this range we use (ch >= 48 && ch <= 57). To check for multiple ranges we use ||(OR) operator. For example, for special symbol:


(ch >= 0 && ch <= 47) ||
(ch >= 58 && ch <= 64) ||
(ch >= 91 && ch <= 96) ||
(ch >= 123 && ch <= 127)

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 *