题目描述
从键盘输入一个字符,根据输入字符的ASCII码判断字符类型。
ASCII码值小于32的为控制字符,输出"This is a control character.";
在'0'到'9'之间的为数字,输出"This is a digit.";
在'A'到'Z'之间的为大写字母,输出"This is a capital letter.";
在'a'到'z'之间的为小写字母,输出"This is a small letter.";
其余为其它字符,输出"This is an other character."
解析:
题目上把字符分成了5部分:控制字符、0~9的数字、大写字母、小写字母、其他字符。我们定义变量,要选择字符类型 char。“判断”即选择,所以我们可以用 If 条件语句来判断。
详细代码:
#include<stdio.h>
int main()
{
char letter;
scanf("%c",&letter);
if(letter<=32)
{
printf("This is a control character.");
}
else if((letter>='0')&&(letter<='9'))
{
printf("This is a digit.");
}
else if((letter>='a')&&(letter<='z'))
{
printf("This is a small letter.");
}
else if((letter>='A')&&(letter<='Z'))
{
printf("This is a capital letter.");
}
else printf("This is an other character.");
return 0;
}