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

来源:互联网 发布:migration数据库 编辑:程序博客网 时间:2024/06/10 12:24
 

//统计各个数字、空白符(包括空格、制表符及换行符)及所有其他字符出现的次数

 #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}; 可以代替上述程序中的数组赋值方法

原创粉丝点击