统计各个数字、空白符、及所有其他字符出现的次数
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}; 可以代替上述程序中的数组赋值方法
#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}; 可以代替上述程序中的数组赋值方法
相关文章推荐
- 4.统计各个数字,空白符,以及其他字符出现的次数
- 编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数
- 统计字符串中:各个数字、空白字符、以及其他所有字符出现的次数
- 编写一个程序统计输入字符串中:各个数字,空白字符,以及其他所有字符常出现的次数。
- 统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数
- C语言:编写一个程序统计输入字符串中,各个数字、空白字符、以及其他所有字符出现的次数。
- K&R《C程序设计语言》p15:统计各个数字、空白符及其他字符出现的次数
- 编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数
- 【C语言】编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数。
- C语言:编写一个程序统计输入字符串中,各个数字、空白字符、以及其他所有字符出现的次数。
- 统计字符串中:各个数字、空白字符、以及其他所有字符出现的次数
- 统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数
- 从终端输入中统计数字,空白符,其他字符出现次数
- 【C语言】编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数。
- 统计字符串里面数字,字母,其他字符的出现次数
- 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
- 练习三:统计字符串中字符中 大写字符 小写字符 数字 其他字符 出现的次数
- 编写一个程序统计各个数字、空白符(包括空格符、制表符及换行符)以及其它字符出现的次数
- 用switch...case语句统计数字、空格和其他字符出现的次数
- linux实现针对文本统计字母出现的次数(所有的可打印的字符)