问答题 下列给定程序中,函数fun的功能是:计算s所指字符串中含有t所指字符串的数目,并作为函数值返回。 请改正程序中的错误或在下画线处填上正确的内容并把下画线删除,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include<stdlib.h> #include<conio.h> #include<string.h> #include<stdio.h> #define N 80 int fun(char*s,char*t) { int n; char*P,*r; n=0 ; /*********found*********/ *r=t; while(*S) { p=s ; while(*r) { if(*r==*P) { r++; p++; } else break; if(*r=='\0') n++; } /*********found*********/ 1 ; s++; } return n; } void main() {char a[N],b[N];int m; printf("\nPlease enter string a:"); gets(a); printf("\nPlease enter substring b:"); gets(b); m=fun(a,b); printf("\nThe result is:m=%d\n",m); }
【正确答案】正确答案:(1)r=t; (2)r=t;或r=&t[0];
【答案解析】解析:从字符串s中找出子字符串的方法是:从第一个字符开始,对字符串进行遍历,若s串的当前字符等于t串的第一个字符,两字符串的指针自动加1,继续比较下一个字符;若比较至字符串t的末尾,则跳出循环;若s串的字符与t串的字符不对应相同,则继续对s串的下一个字符进行处理。