题目:
从键盘输入两个长度小于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 str: abefsfl↙
Input the second str: befs↙
Yes
程序运行结果示例3:
Input the first str: aAbde↙
Input the second str: abc↙
No
输入第一个字符串的提示信息: ““Input the first string:””
输入第二个字符串的提示信息: ““Input the second string:””
输入单个字符的提示信息: ““Input a character:\n””
输入格式: 用 gets()函数
输出格式:
是子串,输出: ““Yes\n””
不是子串,输出: ““No\n””
解答:
#include <stdio.h>
#define N 80
int IsSubString(char a[], char b[]);
int IsSubString(char a[], char b[])
{
int i=0,j=0,len1,len2;
len1=strlen(a);
len2=strlen(b);
while(i<len1&&j<len2)
{
if(a[i]==b[j])
{
i++;j++;
}
else
{
i=i-j+1;j=0;
}
}
if(j==len2)
return 1;
else
return 0;
}
int main()
{
int a[N],b[N],t;
printf("Input the first string:");
gets(a);
printf("Input the second string:");
gets(b);
t=IsSubString(a,b);
if(t)
printf("Yes\n");
else
printf("No\n");
}