C求数字字符串中各个数字出现的次数
1、打开Dev-C++。
2、#include<stdio.h>#include<string.h>void main() {}
3、#include<stdio.h>void main() { char a[100]; int acount[10]={0},i; gets(a像粜杵泳); for(i=0; a[i]!='\0'; i++) { acount[a[i]-'0']++; } for(i=0; i<10; i++) { printf("%d=>%d\n",i,acount[i]); }}
4、给代码添加注释。#include<stdio.h>void main() { //声明字符串数组 艘早祓胂char a[100]; //声明保存各个数字出现次数的数组 int acount[10]={0},i; gets(a); //遍历字符串数组,统计各个数字出现的次数 for(i=0; a[i]!='\0'; i++) { acount[a[i]-'0']++; } //输出各个数字以及出现的个数 for(i=0; i<10; i++) { printf("%d=>%d\n",i,acount[i]); }}
5、运行结果,如下所示。
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。