3.子串判断(4分)

3
子串判断 (4分)

题目内容:从键盘输入两个长度小于80的字符串AB,且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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值