Home >Backend Development >C++ >Check if input character is letter, number or special character in C

Check if input character is letter, number or special character in C

PHPz
PHPzforward
2023-09-01 23:29:051421browse

Check if input character is letter, number or special character in C

In this section, we will see how to check whether a given character is a number, letter or some special character in C.

The alphabet is from A – Z and a – z, then the numbers are 0 – 9. All other characters are special characters. So if we check the conditions using these we can find them easily.

Example

#include <stdio.h>
#include <conio.h>
main() {
   char ch;
   printf("Enter a character: ");
   scanf("%c", &ch);
   if((ch >= &#39;A&#39; && ch <= &#39;Z&#39;) || (ch >= &#39;a&#39; && ch <= &#39;z&#39;))
      printf("This is an alphabet");
   else if(ch >= &#39;0&#39; && ch <= &#39;9&#39;)
      printf("This is a number");
   else
      printf("This is a special character");
}

Output

Enter a character: F
This is an alphabet

Output

Enter a character: r
This is an alphabet

Output

Enter a character: 7
This is a number

Output

Enter a character: #
This is a special character

The above is the detailed content of Check if input character is letter, number or special character in C. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:callback function in CNext article:callback function in C