这道C语言咋做?牛B人看

来源:百度知道 编辑:UC知道 时间:2024/05/24 11:44:04
1、给定程序中,函数fun的功能是:将形参s所指字符串中的数字字符转换成对应的数值,计算出这些数值的累加和作为函数值返回。例如形参s所指的字符串为abs5def126jkm8,程序执行后的输出结果为22。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确结果。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int fun(char *s)
{ int sum=0;
while(*s) {
/**********found**********/
if( isdigit(*s) ) sum+= *s- __1__ ;
/**********found**********/
__2__;
}
/**********found**********/
return __3__ ;
}
main()
{ char s[81]; int n;
printf("\nEnter a string:\n\n"); gets(s);
n=fun(s);
printf("\nThe result is: %d\n\n",n);
}

答案如下:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int fun(char *s)
{ int sum=0;
while(*s) {
/**********found**********/
if( isdigit(*s) ) sum+= *s- '0' ;
/**********found**********/
s++;
}
/**********found**********/
return sum;
}
main()
{ char s[81]; int n;
printf("\nEnter a string:\n\n"); gets(s);
n=fun(s);
printf("\nThe result is: %d\n\n",n);
}