C语言编程>第十四周 ③ 请补充fun函数,该函数的功能是:按 “0”到 “9”统计一个字符串中的奇数数字字符各自出现的次数,结果保存在数组num中。注意:不能使用字符串库函数。

例题:请补充fun函数,该函数的功能是:按 “0”到 “9”统计一个字符串中的奇数数字字符各自出现的次数,结果保存在数组num中。注意:不能使用字符串库函。

例如:输入:glters4543123564879fgfd,结果为:1=2,3=2,5=2,7=1,9=1。
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

代码如下:

#include<conio.h>
#include<stdio.h>
#define N 500
void fun(char*t,int count[])
{
    
    
	int i,j;
	int a[10];
	char*p=t;
	for(i=0;i<10;i++)
	{
    
    
		count[i]=0;
		a[i]=0;
	}
	while(*p)
	{
    
    
		if(*p>='0'&&*p<='9')
		a[*p-'0']++;
		p++;
	}
	for(i=1,j=0;i<10;i=i+2,j++)
		count[j]=a[i];
}
main()
{
    
    
	char s[N];
	int count[10],i;
	printf("\nPlease enter a char string:");
	gets(s);
	printf("\nThe original string:");
	puts(s);
	fun(s,count);
	printf("\nThe countber of leter:\n");
	for(i=0;i<5;i++)
	{
    
    
		printf("\n");
		printf("%d=%d",2*i+1,count[i]);
	}
	printf("\n");
}

输出运行窗口如下:
在这里插入图片描述

越努力越幸运!
加油,奥力给!!!

猜你喜欢

转载自blog.csdn.net/qq_45385706/article/details/112060899
今日推荐