通过字符的字母和数字的ASCII递增的特点来判断。
例如:
//参考代码如下:
#include "iostream"
#include "stdio.h"
#include
using namespace std;
int main()
{
char ch;
cin>>ch;
if(ch>='0'&&ch<='9')
cout<<"该字符为数字"<
cout<<"该字符为字母"<
}
/*
运行结果:
1
该字符为数字
g
该字符为字母
E
该字符为字母
*/
按字符或字符串读入,然后按字符AscII码判断
例如:
char st[21];
int i,b;
scanf("%s",st);
i=0;
b=1;
while(st[i]!='\0'){
if((st[i]<'0' || st[i]>'9')&&(st[i]!='.')){
b=0;
break;
}
++i;
}
if(b==0)
printf("不是数字\n");
else
printf("是一个数字\n");