3
子串判断
(4分)
题目内容:从键盘输入两个长度小于80的字符串A和B,且A的长度大于B的长度,编程判断B是不是A的子串,如果是,则输出”Yes”,否则输出”No”。这里所谓的该串的子串是指字符串中任意多个连续的字符组成的子序列。
函数原型:int IsSubString(char a[], char b[]);
函数功能:判断b是否是a的子串,是则返回1,否则返回0
程序运行结果示例1:
Input the first string:Abcdefghijk123↙
Input the second string:123↙
Yes
程序运行结果示例2:
Input the first string:abefsfl↙
Input the second string:befs↙
Yes
程序运行结果示例3:
Input the first string:aAbde↙
Input the second string:abc↙
No
输入第一个字符串的提示信息: "Input the first string:"
输入第二个字符串的提示信息: "Input the second string:"
输入格式: 用 gets()函数
输出格式:
是子串,输出: "Yes\n"
不是子串,输出: "No\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h>
#include <string.h>
#define MAXS 80
char *search(char *s, char *t);
int main()
{
char s[MAXS], t[MAXS], *pos;
printf("Input the first string:");
gets(s);
printf("Input the second string:");
gets(t);
pos = search(s, t);
if (pos != NULL)
printf("Yes\n");
else
printf("No\n");
return 0;
}
char *search(char *s, char *t) {
int i, j, k = 0, slen, tlen;
char *p = NULL;
slen = strlen(s);
tlen = strlen(t);
for (i = 0; i<slen; i++) {
j = i;
while (s[j] == t[k]) {
k++;
j++;
}
if (k >= tlen) {
p = &s[i];
return p;
}
k = 0;
}
return p;
}