Write a C program to Check alphabet, digit or special character

Topics

  • In this program, we first declare a variable c of data type char. We then prompt the user to enter a character using the printf and scanf functions.
  • Next, we use a series of if-else statements to determine if the entered character is an alphabet, digit, or special character. The first condition checks if the character is an alphabet by checking if it falls within the ASCII range for uppercase and lowercase letters. If the character is an alphabet, the program prints the message “c is an alphabet.”.
  • If the character is not an alphabet, the next condition checks if it is a digit by checking if it falls within the ASCII range for numbers. If the character is a digit, the program prints the message “c is a digit.”.
  • Finally, if the character is neither an alphabet nor a digit, it is assumed to be a special character and the program prints the message “c is a special character.”.
#include <stdio.h>
int main() {
    char c;
    printf("Enter a character: ");
    scanf("%c", &c);
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
        printf("%c is an alphabet.", c);
    }
    else if (c >= '0' && c <= '9') {
        printf("%c is a digit.", c);
    }
    else {
        printf("%c is a special character.", c);
    }
    return 0;
}

OUTPUT

Enter a character: A
A is an alphabet.


Enter a character: 9
9 is a digit.


Enter a character: #
# is a special character.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *