0

Write a program to enter a character abd check whether it is alphabet or digit

@@

1st Aug 2018, 12:49 PM
Siddhi Agarwal
7 Respuestas
+ 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.
1st Aug 2018, 1:01 PM
Alexandru Turculet
Alexandru Turculet - avatar
+ 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; }
1st Aug 2018, 12:58 PM
John Wells
John Wells - avatar
+ 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.
1st Aug 2018, 1:01 PM
John Wells
John Wells - avatar
+ 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') //...
1st Aug 2018, 1:44 PM
Babak
Babak - avatar
+ 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.
1st Aug 2018, 12:57 PM
Alexandru Turculet
Alexandru Turculet - avatar
+ 1
If the user inputs a special character then it will be considered as a digit or an alphabet??
1st Aug 2018, 12:59 PM
Siddhi Agarwal
+ 1
tysm
1st Aug 2018, 1:23 PM
Siddhi Agarwal