判断一个字符属于什么类型
例:我们初始化一个字符型变量,然后输入它的值,判断它属于什么类型,control类型还是digit类型等;
int main()
{
char ch='\0';
printf("please input a character:");
ch=getchar();//此处可以用getchar尽量少用scanf,不易出错;
if(ch<=32)
{
printf("this is a control character\n");
}
else if(ch>='0'&&ch<='9')
{
printf("this is a digal\n");
}
else if(ch>='a'&&ch<='z')
{
printf("this is a samll letter\n");
}
else if(ch>='A'&&ch<='Z')
{
printf("this is a capitall letter\n");
}
else
{
printf("this is an other character\n");
}
return 0;
}
但是这样我们看起来不高级,所以我们调用库函数#include,然后就会使程序变的高级一点,主要是想告诉大家,能调用库函数的时候尽量调用,代码如下:
int main()
{
char ch = '\0';
printf("please input a character:");
ch = getchar();//此处可以用getchar尽量少用scanf,不易出错;
if (iscntrl(ch))
{
printf("this is a control character\n");
}
else if (isdigit(ch))
{
printf("this is a digal\n");
}
else if (islower(ch))
{
printf("this is a samll letter\n");
}
else if (isupper(ch))
{
printf("this is a capitall letter\n");
}
else
{
printf("this is an other character\n");
}
return 0;
}