C求数字字符串中各个数字出现的次数

2025-10-21 02:24:46

1、打开Dev-C++。

C求数字字符串中各个数字出现的次数

2、#include<stdio.h>

#include<string.h>

void main() {

}

C求数字字符串中各个数字出现的次数

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]);

    }

}

C求数字字符串中各个数字出现的次数

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]);

    }

}

C求数字字符串中各个数字出现的次数

5、运行结果,如下所示。

C求数字字符串中各个数字出现的次数

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢