C Program to check the type of character entered

CServer Side ProgrammingProgramming

Write a program to find out that a given character is upper case, lower case, number or special character.

Solution

  • If an entered character is capital letter then, it displays the upper case.
Example: Input =H
Output: upper case letter
  • If an entered character is small letter then, it displays the lower case letter.
Example: Input= g
Output: lower case letter
  • If an entered character is number then, it displays the digit.
Example: Input=3
Output: digit
  • If an entered character is a special character then, it displays the special character.
Example: Input= &
Output: special character

Algorithm

Refer an algorithm given below to find out that a given character is upper case, lower case, number or special character.

Step 1 − Read input character from console at runtime.

Step 2 − Compute ASCII value of the character.

Step 3 − If the ASCII value of the character is in the range of 65 and 90, Then, print "Upper Case letter".

Step 4 − If the ASCII value of the character is in the range of 97 and 122, Then, print "Lower Case letter".

Step 5 − If the ASCII value of the character is in the range of 48 and 57, Then, print "Number".

Step 6 − Else, print "Symbol".

Example

Following is the C program to find out that a given character is upper case, lower case, number or special character −

 Live Demo

#include<stdio.h>
int main(){
   char ch;
   printf("enter a character:");
   scanf("%c",&ch);
   if(ch >= 65 && ch <= 90)
      printf("Upper Case Letter");
   else if(ch >= 97 && ch <= 122)
      printf("Lower Case letter");
   else if(ch >= 48 && ch <= 57)
      printf("Number");
   else
      printf("Symbol");
   return 0;
}

Output

When the above program is executed, it produces the following output −

Run 1:
enter a single character:45
Number
Run 2:
enter a character:#
Symbol
Run 3:
enter a character:M
Upper Case Letter
raja
Published on 26-Mar-2021 07:30:53
Advertisements