c语言中统计字符串中数字出现的次数

c语言中统计字符串中数字出现的次数。

1、

#include <stdio.h>

void count(char x[], int y[])
{
    int i = 0;
    while(x[i])
    {
        if(x[i] >= '0' && x[i] <= '9')
            y[x[i] - '0']++;
        i++;
    }
}

int main(void)
{
    char str[128];
    printf("str: "); scanf("%s", str);
    int a[10] = {0};
    count(str, a);
    int i;
    for(i = 0; i < 10; i++)
    {
        printf("'%d' : %d\n", i, a[i]); 
    }
    return 0;
}

 

posted @ 2021-05-27 16:22  小鲨鱼2018  阅读(1261)  评论(0编辑  收藏  举报