All characters whether alphabet, digit or special character have ASCII value. Input character from the user will determine if it’s Alphabet, Number or Special character.ASCII value ranges- Show
Examples :
Output : Article Tags : Write a C program to input a character from user and check whether given character is alphabet, digit or special character using if else. How to check if a character is alphabet, digits or any other special character using if else in C programming. Logic to check alphabet, digit or special character in C programming. Example Output 3 is digitRequired knowledgeBasic C programming, Relational operators, Logical operators, If else Logic to check alphabet, digit or special character
Step by step descriptive logic to check alphabet, digit or special character.
Let us implement the above logic in a C program. Program to check alphabet, digit or special character/** * C program to check alphabet, digit or special character */ #include <stdio.h> int main() { char ch; /* Input character from user */ printf("Enter any character: "); scanf("%c", &ch); /* Alphabet check */ if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { printf("'%c' is alphabet.", ch); } else if(ch >= '0' && ch <= '9') { printf("'%c' is digit.", ch); } else { printf("'%c' is special character.", ch); } return 0; }Note: You can also use ASCII character codes for checking alphabets, digits or special characters as shown in below program. Program to check alphabet, digit or special character using ASCII value/** * C program to check alphabet, digit or special character using ASCII value */ #include <stdio.h> int main() { char ch; /* Input a character from user */ printf("Enter any character: "); scanf("%c", &ch); if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90)) { printf("'%c' is alphabet.", ch); } else if(ch >= 48 && ch <= 57) { printf("'%c' is digit.", ch); } else { printf("'%c' is special character.", ch); } return 0; }Enter any character: a 'a' is alphabet. Happy coding 😉 |