#include<bits/stdc++.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
#define MAXSTRLEN 255
typedef char SString[MAXSTRLEN+1];
Status StrAssign(SString T, char *chars){
int i;
if (strlen(chars)> MAXSTRLEN)return ERROR;
else {
T[0] = strlen(chars);
for (i = 1; i <= T[0]; i++)T[i] = *(chars + i - 1);
return OK;}
}
int Index(SString S,SString T, int pos){
int i= pos;int j = 1;
while(i <= S[0]&&j<= T[0])
if(S[i]==T[j]){
++i;
++j;}
else{
i=i-j+2;
j=1;
}
if(j>T[0])return i-T[0];
else return 0;
return 0;
}
int main(){
SString S,T;
StrAssign(S,"ababcabcacbab");
StrAssign(T,"abcac");
cout<<"主串和子串在第"<<Index(S,T,1)<<"个字符首次匹配"<<endl;
return 0;
}
c++字符串匹配
最新推荐文章于 2023-10-08 20:52:25 发布