#include <iostream>
#include <string.h>
using namespace std;
#define MAXSTRLEN 255
#define ERROR 0
#define OK 1
typedef int Status;
typedef struct {
char ch[MAXSTRLEN + 1];
int length;
} SString;
Status StrAssign(SString& T, char* chars) {
int len = strlen(chars);
if (len > MAXSTRLEN) return ERROR;
T.length = len;
for (int i = 0; i < len; i++) {
T.ch[i] = chars[i];
}
T.ch[len] = '\0';
return OK;
}
int StrLength(SString S) {
return S.length;
}
Status Concat(SString& T, SString S1, SString S2) {
if (S1.length + S2.length <= MAXSTRLEN) {
strcpy(T.ch, S1.ch);
strcat(T.ch, S2.ch);
T.length = S1.length + S2.length;
return OK;
}
else {
return ERROR;
}
}
Status SubString(SString& Sub, SString S, int pos, int len) {
if (pos < 1 || pos > S.length || len < 0 || pos + len - 1 > S.length) {
return ERROR;
}
for (int i = 0; i < len; i++) {
Sub.ch[i] = S.ch[pos - 1 + i];
}
Sub.ch[len] = '\0';
Sub.length = len;
return OK;
}
int StrCompare(SString& S, SString& T) {
return strcmp(S.ch, T.ch);
}
int Index(SString S, SString T, int pos) {
if (pos >= 1 && pos <= S.length) {
int i = pos - 1;
while (i + T.length <= S.length) {
SString sub;
SubString(sub, S, i + 1, T.length);
if (StrCompare(sub, T) != 0) {
i++;
}
else {
return i + 1;
}
}
}
return 0;
}
int main() {
SString S, T;
StrAssign(S, "lghdeflippedlyylgh");
StrAssign(T, "lyy");
cout << "匹配位序为:" << Index(S, T, 7) << endl;
return 0;
}