C Program to check character is an alphabet, Digit or Special Character.
Check Character:
Write a C Program to Check Character is an Alphabet, Digit or Special Character. The program should accept an character from the user and detect if the given character is Alphabet or Digit or Special Character.
Here are the example input and excepted output of the program.
Example 1
Input:
1 |
Enter character to test : 4 |
Output:
1 |
4 is Digit.. |
Example 2:
Input:
1 |
Enter character to test : M |
Output:
1 |
M is alphabet.. |
Example 3:
Input:
1 |
Enter character to test : & |
Output:
1 |
& is special symbol.. |
Algorithm – Check character is an alphabet, Digit or Special Character :
Here is the algorithm of the character detection program.
- The program should take one character as the input from the user. We are going to store it in the c varaible.
- Then We need to check if the given character is Alphabet, Digit or special character.
- We are going to use the
ctype.h library for checking the character. The
ctype.h library contains the functions like
isalpha() and
isdigit().
- The isalpha(ch) function takes Integer (which will be converted to ASCII value upon passing ) as a input. Returns a non-zero integer value if the given character ch is Alphabet.
- Similarly, The isdigit(ch) function takes Integer as input. and Returns the non-zero Integer value if the given value is a digit.
- By using the isalpha() and isdigit() functions we will check if the given input is an alphabet or Digit.
- If given number is neither alphabet nor Digit, Then it must be Special character.
Check Character 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 31 32 33 34 35 |
#include<stdio.h> #include<ctype.h> int main() { // Create a character variable 'ch' char ch; // Take the input character from user // Store it in the 'ch' variable printf("Enter character to test : "); scanf(" %c", &ch); // We will check if the given character 'ch' is alphabet // by using the 'isalpha()' function // The 'isalpha()' function is available in the ctype.h library // 'isalpha()' function takes character as input and return 'true' if it is alphabet. if(isalpha(ch)) //is alpha available in ctype.h { printf("%c is alphabet..\n",ch); } else if(isdigit(ch)) { // The 'isdigit()' function also available in the 'ctype.h' library. // 'isdigit()' function takes one character as input and returns true if the given character is Digit. printf("%c is Digit..\n",ch); } else { // If the given character is not a 'alhpabet' and 'Digit' then it must be special character. printf("%c is special symbol..\n ",ch); } return 0; } |
Program Output:
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 31 32 |
// Compilling the program using the gcc compiler venkey@venkey$ gcc check-alpha.c // Run the Executable file venkey@venkey$ ./a.out Enter character to test : 1 1 is Digit.. venkey@venkey$ ./a.out Enter character to test : A A is alphabet.. venkey@venkey$ ./a.out Enter character to test : v v is alphabet.. venkey@venkey$ ./a.out Enter character to test : ? ? is special symbol.. venkey@venkey$ ./a.out Enter character to test : * * is special symbol.. venkey@venkey$ ./a.out Enter character to test : Q Q is alphabet.. venkey@venkey$ ./a.out Enter character to test : 4 4 is Digit.. venkey@venkey$ |


