原文: https://www.programiz.com/c-programming/examples/alphabet
要理解此示例,您应该了解以下 C 编程主题:
在 C 编程中,字符变量保存的是 ASCII 值(0 到 127 之间的整数),而不是该字符本身。
小写字母的 ASCII 值为 97 至 122。大写字母的 ASCII 值为 65 至 90。
如果用户输入的字符的 ASCII 值在 97 到 122 或 65 到 90 的范围内,则该数字为字母。
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
printf("%c is an alphabet.", c);
else
printf("%c is not an alphabet.", c);
return 0;
} 输出
Enter a character: *
* is not an alphabet 在程序中,使用'a'代替97,并且使用'z'代替122。 类似地,使用'A'代替65,并且使用'Z'代替90。
注意:建议使用isalpha()函数检查字符是否为字母。