C语言编程>第十六周 ⑧ S是一个由数字和字母字符串组成的字符串,由变量len传入字符串长度。请补充fun函数,该函数的功能是把字符串s中的数字字符转换成数字并存放到整型数组a中

例题:S是一个由数字和字母字符串组成的字符串,由变量len传入字符串长度。请补充fun函数,该函数的功能是把字符串s中的数字字符转换成数字并存放到整型数组a中,函数返回数组a的长度。

例如,s=“Abc123e456hui7890”,结果为:1234567890。
请勿改动主函数main与其它函数中的任何内容,仅在fun函数的横线上填写所需的若干表达式或语句。

代码如下:

#include<stdio.h>
#define M 80
int a[M];
int fun(char s[],int a[],int len)
{
    
    
	int j=0,m=0;
	for(j=0;j<len;j++)
		if(s[j]>='0'&&s[j]<='9')
		{
    
    
			a[m]=s[j]-'0';
			m++;
		}
	return m;
}
main()
{
    
    
	char s[M];
	int len=0,m,j;
	printf("Please input a string:\n");
	gets(s);
	while(s[len])
		len++;
	m=fun(s,a,len);
	printf("\nThe result string:");
	for(j=0;j<m;j++)
		printf("%d",a[j]);
	printf("\n");
}

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

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

猜你喜欢

转载自blog.csdn.net/qq_45385706/article/details/112274145