C Program to Check whether the entered character is capital, small letter, digit or special character

In this tutorial, we will write a program to check whether the entered character is capital, small letter, digit or any special character using C programming.

Here, we will write two different C program,

  1. user library function
  2. ASCII value of the character

1. Using library function

The following C program Check whether the entered character is capital, small letter, digit or special character by using the user library function provided by the ctype.h header file.

Following are some of the library functions in C:

  • isupper(): It returns true if the character is in uppercase.
  • islower(): It returns true if the character is in lowercase.
  • isdigit(): It returns true if entered character is a digit.
  • isalpha(): This function checks whether the passed character is alphabetic.
  • isspace(): This function checks whether the passed character is white-space.
    etc.

C Program:

#include<stdio.h>
#include<conio.h>
#include<ctype.h>

void main()
{
    char ch;
    
    //user input for Character
    printf("Enter a Character: ");
    scanf("%c", &ch);
    
    //check for upper case
    if(isupper(ch))
        printf("%c is an Upper case character.", ch);
    
    //check for lower
    else if(islower(ch))
        printf("%c is a Lower case character", ch);
    
    //check fordigit
    else if(isdigit(ch))
        printf("%c is a Digit character.", ch);
    
    //else it is special character
    else
        printf("%c is a Special character.", ch);
        
    getch();
}

Output:

//First Execution
Enter a Character: A
A is an Upper case character.

//Second Execution
Enter a Character: b
b is a Lower case character.

//Third Execution
Enter a Character: 7
7 is a Digit character.

//Fourth Execution
Enter a Character: @
@ is a Special character.


2. ASCII value of the character

The following C Program To Check For Alphabet, Number, and Special Symbol using the ASCII value.

The following table shows the range of ASCII values for various characters.

CharactersASCII Values
A – Z65 – 90
a – z97 – 122
0 – 948 – 57
special symbols0 – 47, 58 – 64, 91 – 96, 123 – 127

C Program:

#include <stdio.h>

int main()
{
  char ch;

  printf("Enter a Character: ");
  scanf("%c", &ch);

  if (ch >= 65 && ch <= 90)
    ("%c is an Upper case Character.", ch);

  else if (ch >= 97 && ch <= 122)
    printf("%c is a lower case Character.", ch);

  else if (ch >= 48 && ch <= 57)
    printf("%c is a Digit Character.", ch);

  else if ((ch >= 0 && ch <= 47) ||
    (ch >= 58 && ch <= 64) ||
    (ch >= 91 && ch <= 96) ||
    (ch >= 123 && ch <= 127))
    printf("%c is a Special Character.", ch);

  return 0;
}

Output:

//Run 1
Enter a Character: 7
7 is a Digit Character.

//Run 2
Enter a Character: A
A is an Upper case Character.