0
Write a program to enter a character abd check whether it is alphabet or digit
@@
7 Answers
+ 1
Siddhi Agarwal In the program John Wells posted if the user inputs something that isn't a digit or a letter it will show "other". The program he wrote is in C, so you could try to play with it in playground.
+ 5
This program does that:
#include <stdio.h>
int main() {
char c;
scanf("%c", &c);
if (c >= 48 && c <= 57)
printf("digit\n");
else if (c >= 65 && c <= 90 || c >= 97 && c <= 122)
printf("alphabetic\n");
else
printf("other\n");
return 0;
}
+ 4
If it is 0 to 9, it is digit. If a to z or A to Z, it is alphabetic. Otherwise, it is neither so other.
+ 3
Excuse me Mr. Wells , I'm not in the position to critique your craft but wouldn't it be more readable to explicitly indicate the range of alphanumeric values by their char representation? as
//...
if (c >= '0' && c <= '9')
//...
else if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')
//...
+ 1
Search for the ASCII table and you'll find that all letters and digits are just a number, so you could check if it's less than or bigger than some values and you'll be done. If you want a demo, respond here.
+ 1
If the user inputs a special character then it will be considered as a digit or an alphabet??
+ 1
tysm