Menu Close

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.
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Leave the field below empty!

Posted in C 决策和循环语句, C语言习题集

Related Posts