数组

/******************************************************/
/*统计各个数字、空白符及其他字符出现的次数*/
#include <stdio.h>
main()
{
 int c,i,nwhite,nother;
 int ndigit[10];
 
 nwhite = nother = 0;
 for(i=0;i<10;++i)
  ndigit[i] = 0;
 
 while((c = getchar()) != EOF)
  if(c >= '0' && c <= '9')
   ++ndigit[c-'0'];
  else if (c == ' ' || c == ' ' || c == ' ')
   ++nwhite;
  else
   ++nother;
 
 printf("digits =");
 for(i = 0;i < 10;++i)
  printf(" %d",ndigit[i]);
 printf(",white space = %d, other = %d ",nwhite,nother); 
}

一个字符串中,存储的是字符型的数字,也就是48~57把他变成0~9可以用这个字符型的减去48,也可以用这个字符减去0的字符型的ascii码值,也就是’0‘定义一个是个空间的int型数组如a[10]={0}全部初始化为零,对应着0~9这十个数出现的频率。a[0]~a[9]对应0~9这样看,所以可以用c-‘0‘来使用,计算的数字刚好是0~9 。
 
原文地址:https://www.cnblogs.com/TheFly/p/11821209.html