子串判断(MOOC)

子串判断(MOOC)
题目内容:从键盘输入两个长度小于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”

#include<stdio.h>
#include<string.h>
int IsSubString(char a[], char b[]);
int main()
{
	int flag;//用于判断输入的字符串是否在原串中 
	char a[80],b[80];
	printf("Input the first string:");
	gets(a);
	printf( "Input the second string:");
	gets(b);
	flag = IsSubString(a,b);
	if(flag){
		printf("Yes\n");
	}else{
		printf("No\n");
	}
	return 0;
} 
int IsSubString(char a[], char b[]){
	int i=0,len = strlen(b),len1=strlen(a);
	for(int i=0; i <= len1-len;i++){
		if(a[i]==b[0]){
			int count = 1;
			for(int j=1; j<len; j++){
				if(a[i+j]==b[j]){
					count ++;
				}
			}
			if(count==len){
				return 1;
			}
		}
	}
	
	return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值