问答题

下列给定程序中,函数fun()的功能是:从s所指字符串中,找出t所指字符串的个数作为函数值返回。例如,当s所指字符串中的内容为abcdabfab,t所指字符串的内容为ab,则函数返回整数3。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构.
试题程序:
#include <conio.h>
#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)
/**************found**************/
if(*r==*p) r++; p++
else break;
/*************found**************/
if(r==’\0’)
n++;
s++;

return n;

main()
char s[100], t[100]; int m;
clrscr();
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);

【参考答案】

(1)错误:if(*r==*p) {r++;p++} 正确:if(*r==*p) {r++;p++;}
(2......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

填空题
下列给定程序中,函数fun()的功能是:对N名学生的学习成绩,按从高到低的顺序找出前m(m≤10)名学生来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。 试题程序: #include<stdio.h> #include<malloc.h> #include<string.h> # include<conio.h> #define N 10 typedef struct ss char num[10]; int order; STU; STU *fun(STU a[], iht m) STU b[N], *tt; int i,j,k; 【1】 ; for(i=0; i<N; i++) b[i]=a[i]; for (k=0;k<m;k++) for (i=j=0;i<N;i++) if ( 【2】 ) j=i; tt [k]=b[j]; b[j].order=0; return 【3】 ; outresult (STU a[],FILE *pr) int i; for(i=0;i<N; i++) fprintf(pf, No=%s Mark=%d n , a[i].num,a[i].order); fprintf(pf, n n ); main() STU [N]= A01 ,80, A02 ,79, A03 ,66, A04 ,82, A05 ,87, A06 ,93, A07 ,78, A08 ,60, A09 ,85, A10 ,73; STU *p_order; int i,m; clrscr(); printf( *** The Origial data *** n ); outresult(a, stdout); printf( nGive the numeber of the students who have better score: ); scanf( %d ,&m); while (m>10) printf( nGive the number of the studets who have better score: ); scanf( %d ,&m); p_order=fun(a,m); printf( *** THE RESULT *** n ); printf( *** The top students *** n ); for(i=0; i<m; i++) printf ( %s %d n , p_order[i].num,p_order[i].order); free(p_order);