二级C语言
第1221题
给定程序MODI1.C中函数fun的功能是:从s所指字符串中,找出与t 所指字符串相同的子串的个数作为函数值返回。 例如,当s所指字符串中的内容为:“abcdabfab”,t所指字符串的 内容为:“ab”,则函数返回整数3。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的 结构!
/**********code.c**********/
#include <stdio.h>
#include <string.h>
int fun (char *s, char *t)
{
int n;
char *p,*r;
n = 0;
while(*s)
{
p = s;
r = t;
while (*r)
if(*r == *p)
{
/**********found**********/
r++; p++
}
else break;
/**********found**********/
if(r == '\0')
n++;
s++;
}
return n;
}
void main()
{
char s[100],t[100];
int m;
printf("\nPlease enter string S:");
scanf("%s", s);
printf("\nPlease enter substring t:");
scanf("%s", t);
m = fun(s,t);
printf("\nThe result is: m = %d\n", m);
}
/**********-code.c**********/
第1222题
函数fun的功能是:将s所指字符串中ASCII值为偶数的字符删除, 串中剩余字符形成一个新串放在t所指的数组中。 例如,若s所指字符串中的内容为:“ABCDEFGl2345”,其中字符 B的ASCII码值为偶数、…、字符2的ASCII码值为偶数、…都应当删 除,其它依此类推。最后t所指的数组中的内容应是:“ACEG135”。 注意:部分源程序存在文件PROG1.c中。 请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花 括号中填入你编写的若干语句。
/**********code.c**********/
#include <stdio.h>
#include <string.h>
void fun(char *s,char t[])
{
}
void main()
{
char s[100],t[100];
printf("\nPlease enter string S:");
scanf("%s",s);
fun(s,t);
printf("\nThe result is: %s\n", t);
}