C 语言检查字符是字母、数字还是特殊字符

如何编写一个 C 语言程序来检查字符是字母、数字还是特殊字符,并附带示例。为此,我们将使用内置函数 isalpha、isdigit 和 ASCII 码。

使用 else if 语句检查字符是字母、数字还是特殊字符的 C 语言程序

此 C 语言程序允许用户输入一个字符。然后,它将检查该字符是字母、数字还是特殊字符。

在此示例中,我们将使用 编程 内置函数 isalphaisdigit 来查找字符是字母还是数字。如果两个条件都不满足,则为特殊字符。

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

int main()
{
char ch;

printf(" Please Enter any character : ");
scanf("%c", &ch);

if (isalpha(ch))
{
printf("\n %c is an Alphabet", ch);
}
else if (isdigit(ch))
{
printf("\n %c is a Digit", ch);
}
else
printf("\n %c is a Special Character", ch);

return 0;
}

字母、数字或特殊字符输出。

C Program to Check Character is Alphabet Digit or Special Character  using isalpha, isdigit

让我们检查 9 是字母、数字还是特殊字符

 Please Enter any character :  9

 9 is a Digit

让我们检查特殊字符

 Please Enter any character :  @

 @ is a Special Character

检查字符是字母、数字还是特殊字符的示例 2

在此程序中,为了检查字母、数字或特殊字符,我们将字母和数字直接放在 Else If 语句 中。

 #include <stdio.h>

int main()
{
char ch;

printf(" Please Enter any character : ");
scanf("%c", &ch);

if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') )
{
printf("\n %c is an Alphabet", ch);
}
else if (ch >= '0' && ch <= '9')
{
printf("\n %c is a Digit", ch);
}
else
printf("\n %c is a Special Character", ch);

return 0;
}
Character is Alphabet, Digit, or Special Character  using else if statement

使用 ASCII 值检查字符是字母、数字还是特殊字符

在此 程序 中,我们将使用 ASCII 表 值来检查输入的字符是字母、数字还是特殊字符。

 #include <stdio.h>

int main()
{
char ch;

printf(" Please Enter any character : ");
scanf("%c", &ch);

if (ch >= 48 && ch <= 57)
{
printf("\n %c is a Digit", ch);
}
else if ( (ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) )
{
printf("\n %c is an Alphabet", ch);
}
else
printf("\n %c is a Special Character", ch);

return 0;
}

让我们检查 3 是否为数字

 Please Enter any character :  3

 3 is a Digit

让我们检查 $ 是否为特殊字符

 Please Enter any character :  $

 $ is a Special Character