|
问题描述:
编写一个程序统计输入字符串中:各个数字、空白字符、以及其他所有字符出现的次数。
代码实现:
?
#include
#include
int main()
{
int other=0;
int space=0;
int a[10]={0};
int i=0;
int ch=0;
while ((ch=getchar())!=EOF)
{
/*
//isspace库函数---判断输入字符ch是不是空,如果是,if条件为真。
*/
if (isspace(ch))
{
space++;
}
/*
//isdigit库函数---判断输入字符ch是不是数字,如果是,if条件为真。
*/
else if (isdigit(ch))
{
a[ch-'0']++;
}
else
{
other++;
}
}
printf("space=%d\n",space);
for (i=0;i<10;i++)
{
printf("%d:%d\n",i,a[i]);
}
printf("other=%d\n",other);
}
?
结果是:
|