Write a C Program to Check character is alphabet or number or a special character. The program should accept a 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 totest:4
Output:
1
4isDigit..
Example 2:
Input:
1
Enter character totest:M
Output:
1
Misalphabet..
Example 3:
Input:
1
Enter character totest:&
Output:
1
&isspecial symbol..
Algorithm – Check character is alphabet or Digit or a Special character :
Here is the algorithm of the character detection program.
The program should take one character as 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 a 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 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 the given number is neither alphabet nor Digit, Then it must be a Special character.
Check character is alphabet or number or a Special 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>
intmain()
{
// Create a character variable 'ch'
charch;
// 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);
}
elseif(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);
}
return0;
}
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 totest:1
1isDigit..
venkey@venkey$./a.out
Enter character totest:A
Aisalphabet..
venkey@venkey$./a.out
Enter character totest:v
visalphabet..
venkey@venkey$./a.out
Enter character totest:?
?isspecial symbol..
venkey@venkey$./a.out
Enter character totest:*
*isspecial symbol..
venkey@venkey$./a.out
Enter character totest:Q
Qisalphabet..
venkey@venkey$./a.out
Enter character totest:4
4isDigit..
venkey@venkey$
Method 2: character is alphabet or number or a special character Program:
In the previous method, We used the in-built functions like
isalpha and
isdigit. In this method, we are going to check the alphabet and digit manually.
Logic to check the alphabet:
We are going to compare the ASCII characters. To be an alphabet, the given character must be in between the
a and
z ( upper case and Lower case). So we can use the following logic to check the alphabet
2 Responses
[…] C Program to check character is Alphabet or Numbre or Special Character […]
[…] C Program to check character is Alphabet or Number of Special Number […]