Description
给定两个字符串,找出第2个字符串在第1个字符串中出现的所有位置。
Input
有多组数据,每组数据由两个字符串组成。
Output
第2个字符串在第1个字符串中出现的所有位置,每个数占3位宽度。如果第1个串中没有第2个串,则输出-1。
Sample Input
Harbin University of Science and Technology
ty of
aaaabsbsdde
aa
Harbin
ha
Sample Output
15
0 1 2
-1
//查找子串
#include<stdio.h>
#include<string.h>
int main()
{
int num=-1;
char str[1000];
char str1[1000];
while(gets(str1)){
gets(str);
int n=0;
if(strstr(str1,str)==NULL)
{
printf("%3d\n",num);
}
else
{
while(strstr(str1+n,str)!=NULL)
{
char *p=strstr(str1+n,str);
char c=*p;
*p='\0';
printf("%3d",strlen(str1));
n=strlen(str1);
*p=c;
n++;
}
printf("\n");
}
}
return 0;
}