c check if character is a digit or alphabet
To check if a character is a digit or an alphabet in C, you can use the functions isdigit()
and isalpha()
respectively from the <ctype.h>
library. Here's an example:
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (isdigit(ch)) {
printf("The character is a digit.\n");
} else if (isalpha(ch)) {
printf("The character is an alphabet.\n");
} else {
printf("The character is neither a digit nor an alphabet.\n");
}
return 0;
}
This code prompts the user to enter a character and then checks if it is a digit or an alphabet using the isdigit()
and isalpha()
functions respectively. The appropriate message is then printed based on the result.