从键盘任意输入一个字符,编程判断该字符是数字字符、英文字母、空格还是其他字符。
**输入格式要求:提示信息:"Press akey and then press Enter:"
**输出格式要求:"It is anEnglish character!\n" "It is a digit character!\n" "It is a space character!\n" "It is other character!\n"
程序运行示例1如下:
Press a key andthen press Enter:A
It is an Englishcharacter!
程序运行示例2如下:
Press a key andthen press Enter:2
It is a digitcharacter!
程序运行示例3如下:
Press a key andthen press Enter:
It is a spacecharacter!
程序运行示例4如下:
Press a key andthen press Enter:#
It is othercharacter!
方法一:
#include <stdio.h>
void main()
{
char ch;
ch=getchar();
if(ch >= 'A' && ch <= 'Z' )
{
printf("为大写字符\n");
}
else if(ch >= 'a' && ch <= 'z' )
{
printf("为小写字符\n");
}
else if(ch==' ')
{
printf("为空格字符\n");
}
else
{
printf("为其他字符\n");
}
}
方法二:
#include <stdio.h>
void main()
{
char ch;
int a;
ch = getchar();
if (ch >= 'a' && ch <= 'z')
ch = '1';
if (ch >= 'A' && ch <= 'Z')
ch = '2';
if( ch == ' ')
ch = '3';
switch(ch)
{
case '1' : printf("xiao \n");break;
case '2' : printf("da \n");break;
case '3' : printf("空格 \n");break;
default: printf("qita");
}
}