子串判断(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;
}