Pages

Saturday

C Program Find a Character is No Letter or Special Character

Here is a C program to find whether an entered character is number, letter or special character with proper explanation and output. This program makes use of If Else in C.

# include <stdio.h>
# include <conio.h>
void main()
{
  char ch ;
  clrscr() ;
  printf("Enter a charatcer : ") ;
  scanf("%c", &ch) ;
  if (ch >= 65 && ch <= 90)
    printf("\nEntered character is a upper case letter") ;
  else if(ch >= 97 && ch <= 122)
    printf("\nEntered character is a lower case letter") ;
  else if(ch >= 48 && ch <= 57)
    printf("\nEntered character is a number") ;
  else
    printf("\nEntered character is a special character") ;
  getch() ;
}


Output of above program

Enter a charatcer : G
Entered character is a upper case letter

Enter a charatcer : c
Entered character is a lower case letter

Enter a charatcer : $
Entered character is a special character

Enter a charatcer : 2
Entered character is a number


Explanation of above program

This program uses one of the most flexible features provided by C language i.e. we can use any character as integer and vice-verse. When we compare a character with an integer, C handles this by casting that character to integer by using that character's ASCII value.

In this program, first user enters any character. Then using an IF-ELSE condition, that character's ASCII value is checked to determine whether it is a number, letter or special character.

If entered character's ASCII value is between 65 to 90 then that character is a upper case letter. If entered character's ASCII value is between 97 to 122 then that character is a lower case letter.
If entered character's ASCII value is between 48 to 57 then that character is a number. For rest of the values, that character is a special character.

No comments:

Post a Comment