输入一行数字,统计其中的各类字符
关于这个代码我是加了fgets函数来控制输入流,因为在scanf函数的话,空格会被当成'\0'来对待
所以为了程序的稳定性
就要加入一些专业的字符输入流
之前我的博文里边也是提到了fets函数优点,程序的稳定性和可执行性大大提高
各位同新手们可以借鉴一下
代码如下:
#include<stdio.h>
#include<string.h>
int main()
{
char a[100]; char *p;
int zimu=0,kongge=0,shuzi=0,other=0;
printf("Please input the string:\n");
fgets(a,100,stdin);
for (p=a;p-a<100;p++)
{
if (((*p)>'a'&&(*p)<'z')||((*p)>'A'&&(*p)<'Z'))
zimu+=1;
else if ((*p)==' ')
kongge+=1;
else if ((*p)<'9'&&(*p)>'0')
shuzi+=1;
else
other+=1;
}
printf("字母为%d个\n数字为%d个\n空格为%d个\n其他字符为%d个\n");
return 0;
}