Sunday, January 23, 2022

Check whether a character is an alphabet, digit or special character

This program is based on C Programming Decision making concept (use of if..else statement).

Problem definition: Write a C program to check whether a given character is an alphabet, digit or special character.

#include <stdio.h>
int main()  
{  
    char chr;  

    printf("Enter a character: ");  
    scanf("%c", &chr);  
  
    /* logic to check whether it is an alphabet */  
    if((chr >= 'a' && chr<='z') || (chr>='A' && chr<='Z'))  
    {  
        printf("It is an alphabet.\n");
    }  
    else if(chr>='0' && chr<='9') /* to check digit */  
    {  
        printf("It is a digit.\n");  
    }  
    else /* else special character */
    {  
        printf("It is a special character.\n");  
    }  
}

Sample output 1

Enter a character: A
It is an alphabet.

Sample output 2

Enter a character: 5
It is a digit.

Sample output 3
Enter a character: @
It is a special character.