编写一个程序,判定字符串t是否是字符串s的子串。
输入格式:
两个字符串,各占一行,长度不超过100。
第一行是字符串s,第二行是字符串t
输出格式:
输出子串所在的下标,下标从0开始计数。如未找到输出-1。
输入样例:
aababcabcdabcde
abcd
输出样例:
6
#include <stdio.h>
#include <string.h>
int main()
{
char s[101],t[101];
int a=0,b=0;
int i=1,j=0;
while(~scanf("%s%s\n",s,t))
{
while(a<strlen(s)&&b<strlen(t))
{
if(s[a]==t[b])
{
a++;
b++;
}
else
{
a=i++;
b=0;
}if(b==strlen(t))
{
printf("%d\n",i-1);
j=1;
break;
}
}
if(a>=strlen(s)&&j==0)
printf("-1\n");
}
return 0;
}