您的位置:首页 > 其它

统计各个数字、空白符、及所有其他字符出现的次数

2011-09-08 20:27 357 查看
//统计各个数字、空白符(包括空格、制表符及换行符)及所有其他字符出现的次数
#include <stdio.h>
/* count digits, white space, others */
int main()
{
int c, i, nwhite, nother;
int ndigit[10]; //数字
nwhite = nother = 0; //空白符 所有D其他字符
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0']; // easier than switch case
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

for (i = 0; i < 10; ++i)
printf("\n%d出现次数\t%d",i, ndigit[i]);
printf(", white space = %d, other = %d\n",
nwhite, nother);
}
注意:

1, 数组的引用;

2, for (i = 0; i < 10; ++i)
ndigit[i] = 0;
的妙用

3,int ndigit[10]={0}; 可以代替上述程序中的数组赋值方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐