判断输入的字符是不是字母
代码示例:
#include <stdio.h>
int main()
{
char ch = 0;
while (scanf(" %c", &ch) == 1)//%c前面加空格,是为了跳过下一个字符之前的所有空白字符\n
{
if (isalpha(ch))// isalpha函数用于判断是否为字母,是输出1.
{
printf("%c is an alphabet.\n", ch);
}
else
{
printf("%c is not an alphabet.\n", ch);
}
}
return 0;
}
运行结果:
#
# is not an alphabet.
G
G is an alphabet.
c
c is an alphabet.
9
9 is not an alphabet.
分析:
? ? ? ? scanf中%c前面加空格,是为了跳过下一个字符之前的所有空白字符\n
? ? ? ? isalpha函数用于判断是否为字母,是输出1.