C Program to check character is alphabet or digit
C program to check whether given character is alphabet or digit
Write a C program to input a character from user and check whether a given character is alphabet or digit using if...else...if statement. Have a look at the below step by step logic to check whether a given character is alphabet or digit.
Example:
- INPUT
- Enter a character: L
OUTPUT
L is a alphabet character.
Required knowledge for this exercise
Logic to check whether a given character is alphabet or digit:
In C programming, every character has an unique ASCII value (An integer value between 0 to 127 is known as ASCII value).Which is used to represent a character in memory. In memory every character is stored as an integer.
Now let's see the below step by step logic to check given character is alphabet or digit.
1) Input a character from user and store it in variable ch
using scanf() fucntion.
2) Check the below conditions:
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) then, it is an ALPHABET.
if ((ch >= '0' && ch <= '9') then, it is a DIGIT.
Otherwise neither ALPHABET nor DIGIT
Program to check whether a character is alphabet or digit using if...else...if:
-
#include <stdio.h>
int main()
{
char ch;
printf("Please Enter any character: ");
scanf("%c", &ch);
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("\n%c is an Alphabet.", ch);
}
else if ((ch >= '0' && ch <= '9')
{
printf("\n%c is a digit.", ch);
}
- else
{
printf("\n%c neither Alphabet nor digit.", ch);
}
return 0;
}
You can also use the ASCII value to check alphabets character. Since we know that, every character has an unique ASCII value (An integer value between 0 to 127). For example: ASCII value of a=97, z=122, A=65, Z=90
and 0=48, 9=57
.
Have a look at the below program to check whether a character is alphabet or not using ASCII Value:
-
#include <stdio.h>
int main()
{
char ch;
printf("Please Enter any character: ");
scanf("%c", &ch);
if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90))
{
printf("\n%c is an Alphabet.", ch);
}
else if ((ch >= 48 && ch <= 57)
{
printf("\n%c is a digit." ch);
}
- else
{
printf("\n%c neither Alphabet nor digit.", ch);
}
return 0;
}
Output:
- Please Enter any character: L
- L is an alphabet.
- Please Enter any character: 10
- 10 is a digit.
Help others by sharing this page.
Ahmad Irshad
Author & Editor
I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP
Thursday, October 01, 2020
C
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.