C Program to check character is alphabet or number or a Special character

check-character-is-alphabet-or-number-or-a-special-character

Check Character Program :

Write a C Program to Check character is alphabet or number or a special character. The program should accept a character from the user and detect if the given character is Alphabet or Digit or Special Character.

Here are the example input and excepted output of the program.

Example 1

Input:

Output:

Example 2:

Input:

Output:

Example 3:

Input:

Output:

Algorithm – Check character is alphabet or Digit or a Special character :

Here is the algorithm of the character detection program.

  • The program should take one character as input from the user. We are going to store it in the c varaible.
  • Then We need to check if the given character is Alphabet, Digit, or a special character.
  • We are going to use the ctype.h library for checking the character. The ctype.h library contains the functions like isalpha() and isdigit().
    • The isalpha(ch) function takes Integer (which will be converted to ASCII value upon passing ) as input. Returns a non-zero integer value if the given character ch is Alphabet.
    • Similarly, The isdigit(ch) function takes Integer as input. and Returns the non-zero Integer value if the given value is a digit.
  • By using the isalpha() and isdigit() functions we will check if the given input is an alphabet or Digit.
  • If the given number is neither alphabet nor Digit, Then it must be a Special character.

Check character is alphabet or number or a Special character Program:

Program Output:

Check-character-is-an-alphabet-or-digit-or-special-character

Method 2: character is alphabet or number or a special character Program:

In the previous method, We used the in-built functions like isalpha and isdigit. In this method, we are going to check the alphabet and digit manually.

Logic to check the alphabet:

We are going to compare the ASCII characters. To be an alphabet, the given character must be in between the a and z ( upper case and Lower case). So we can use the following logic to check the alphabet

(ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’)

If the above logic returns True, Then the given number is Alphabet.

Logic to check the Digit:

Here also we will compare the ASCII character. Use the following condition to check if the character ch is Digit.

ch >= ‘0’ && ch <= ‘9’

If this condition is True, Then input character ch is Digit.

Check character C Program:

Program Output:

We used GCC compiler to compile and run the program on Linux operating system. Learn more about the C Program compilation in following article

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] C Program to check character is Alphabet or Numbre or Special Character […]

  2. […] C Program to check character is Alphabet or Number of Special Number […]

Leave a Reply