c 中任意读入一个字符串,统计输出其中每一个数字字符出现的次数

如题所述

#include <stdio.h>
int main()
{char s[200];
 int a[10]={0},i;
 gets(s);
 for(i=0;s[i];i++)
   if(s[i]>='0'&&s[i]<='9')
     a[s[i]-'0']++;
 for(i=0;i<10;i++)
   if(a[i])
     printf("%c: %d\n",'0'+i,a[i]);
 return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-24
#include <stdio.h>
#include <map>

int main()
{
    char sLine[1024] = "";
    scanf("%s",sLine);
    std::map<char,int> mapCount;
    for(int i=0;sLine[i]!=0;i++){
        mapCount[sLine[i]]++;
    }
    std::map<char,int>::iterator it = mapCount.begin();
    for(;it!=mapCount.end();it++)
    {
        printf("%c is %d\n",it->first,it->second);
    }
    return 0;
}

本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网