// 串的模式匹配.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int Index(char S[],char T[],int pos)
{
//返回子串T在主串S中第pos个字符之后的位置.若不存在,则函数值为0
//其中,T非空,i<=pos<=StrLength(S).
int i=pos;
int j=1;
int m,n;
m=strlen(S);
n=strlen(T);
/* cout<<m<<" "<<n<<endl;*/
while(i<=m && j<=n){
if(S[i]==T[j]) {++i;++j;} //继续比较后续字符
else {i=i-j+2;j=1;} //指针后退重新开始匹配
}
/* cout<<i<<" "<<j<<endl;*/
if(j>n) return i-n;
else return 0;
}
int main(int argc, char* argv[])
{
//string CS,CT;
char CS[200],CT[100];
int pos;
cout<<"请输入主串"<<endl;
scanf("%s",CS);
cout<<"请输入子串"<<endl;
scanf("%s",CT);
pos=Index(CS,CT,1);
cout<<pos<<endl;
return 0;
}