从键盘接收一个字符,如果是字母,输出其对应的ASCII码,如果是数字,按原样输出,否则给出提示信息输入错误

2024-10-28 03:04:41
推荐回答(2个)
回答1:

#include 
int main()
{
    char character;
    printf("请输入字符:");
    while((character=getchar())!=EOF)
    {
        if(('a'<=character && character<='z')||('A'<=character && character<='Z'))
        {
            printf("字符%c的ACSII码为:%d\n", character, character);
        }
        else if(('0'<=character && character<='9'))
        {
            printf("数字为:%d\n", character-'0');
        }
        else
        {
            printf("输入错误!\n");
        }
        printf("请输入字符:");
        fflush(stdin);
    }
    return 0;
}

运行结果如下:

回答2:

#include
main()
{
char c;
printf("请输入字符:");
scanf("%c",&c);
if(c>='A'&&c<='z')
printf("%c的ACSII码为:%d\n",c);
else
if(c>='0'&&c<='9')
printf("%d\n",c);
else
printf("请重新输入!");
}