In this tutorial, we will write a program to check whether the entered character is capital, small letter, digit or any special character using C programming.
Here, we will write two different C program,
- user library function
- ASCII value of the character
1. Using library function
The following C program Check whether the entered character is capital, small letter, digit or special character by using the user library function provided by the ctype.h
header file.
Following are some of the library functions in C:
isupper():
It returns true if the character is in uppercase.- islower(): It returns true if the character is in lowercase.
- isdigit(): It returns true if entered character is a digit.
- isalpha(): This function checks whether the passed character is alphabetic.
- isspace(): This function checks whether the passed character is white-space.
etc.
C Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char ch; //user input for Character printf("Enter a Character: "); scanf("%c", &ch); //check for upper case if(isupper(ch)) printf("%c is an Upper case character.", ch); //check for lower else if(islower(ch)) printf("%c is a Lower case character", ch); //check fordigit else if(isdigit(ch)) printf("%c is a Digit character.", ch); //else it is special character else printf("%c is a Special character.", ch); getch(); } |
Output:
//First Execution
Enter a Character: A
A is an Upper case character.
//Second Execution
Enter a Character: b
b is a Lower case character.
//Third Execution
Enter a Character: 7
7 is a Digit character.
//Fourth Execution
Enter a Character: @
@ is a Special character.
2. ASCII value of the character
The following C Program To Check For Alphabet, Number, and Special Symbol using the ASCII value.
The following table shows the range of ASCII values for various characters.
Characters | ASCII Values |
A – Z | 65 – 90 |
a – z | 97 – 122 |
0 – 9 | 48 – 57 |
special symbols | 0 – 47, 58 – 64, 91 – 96, 123 – 127 |
C Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdio.h> int main() { char ch; printf("Enter a Character: "); scanf("%c", &ch); if (ch >= 65 && ch <= 90) ("%c is an Upper case Character.", ch); else if (ch >= 97 && ch <= 122) printf("%c is a lower case Character.", ch); else if (ch >= 48 && ch <= 57) printf("%c is a Digit Character.", ch); else if ((ch >= 0 && ch <= 47) || (ch >= 58 && ch <= 64) || (ch >= 91 && ch <= 96) || (ch >= 123 && ch <= 127)) printf("%c is a Special Character.", ch); return 0; } |
Output:
//Run 1
Enter a Character: 7
7 is a Digit Character.
//Run 2
Enter a Character: A
A is an Upper case Character.