给定程序中函数fun的功能是:求出在字符串中最后一次出现的子字符串的地址,通过函数值返回,在主函数中输出从此地址开始的字符串;若未找到,则函数值为NULL。
函数接口定义:
char *fun (char *s, char *t );
其中 s 和 t 是用户传入的参数。函数求 t 指针所指的字符串在s指针所指的字符串中最后一次出现的地址,并返回,若未找到,则函数值为NULL。
裁判测试程序样例:
#include <stdio.h>
char *fun (char *s, char *t );
int main()
{char s[100], t[100], *p;
scanf("%s", s );
scanf("%s", t );
p = fun( s, t );
if (p) printf("The result is : %s\n", p);
else printf("Not found !\n" );
}
/* 请在这里填写答案 */
输入样例:
abcdabfabcdx
ab
输出样例:
The result is : abcdx
输入样例:
abcdabfabcdx
abd
输出样例:
Not found !
代码如下:
#include<string.h>
char* fun(char* s, char* t) {
int str1 = strlen(s), str2 = strlen(t);
int i, h;
char* p = NULL;
for (i = 0; i < str1; i++) {
for (h = 0; h < str2; h++) {
if (*(s + i + h) != *(t + h)) {
break;
}
}
if (h == str2) {
p = s + i;
}
}
return p;
}