头文件的定义
#define MaxSize 100 //最多的字符个数
typedef struct
{ char data[MaxSize]; //定义可容纳MaxSize个字符的空间
int length; //标记当前实际串长
} SqString;
void StrAssign(SqString &s,char cstr[]);
void StrCopy(SqString &s,SqString t);
bool StrEqual(SqString s,SqString t);
int StrLength(SqString s);
SqString Concat(SqString s,SqString t);
SqString SubStr(SqString s,int i,int j);
SqString InsStr(SqString s1,int i,SqString s2);
SqString DelStr(SqString s,int i,int j);
SqString RepStr(SqString s,int i,int j,SqString t);
void DispStr(SqString s);
int Index(SqString s,SqString t); //简单匹配算法
void GetNext(SqString t,int next[]); //由模式串t求出next值
int KMPIndex(SqString s,SqString t); //KMP算法
void GetNextval(SqString t,int nextval[]); //由模式串t求出nextval值
int KMPIndex1(SqString s,SqString t); //修正的KMP算法
include “stdafx.h”
include “sqstring.h”
void StrAssign(SqString &s,char cstr[]) //s为引用型参数
{ int i;
for (i=0;cstr[i]!=’\0’;i++)
s.data[i]=cstr[i];
s.length=i;
}
void StrCopy(SqString &s,SqString t) //s为引用型参数
{ int i;
for (i=0;i
// StringApp.cpp : Defines the entry point for the console application.
//
include “stdafx.h”
include “sqstring.h”
int main(int argc, char* argv[])
{
int j;
int next[MaxSize],nextval[MaxSize];
SqString s,t;
StrAssign(s,"abcabcdabcdeabcdefabcdefg");
StrAssign(t,"abcdeabcdefab");
printf("串s:");DispStr(s);
printf("串t:");DispStr(t);
printf("简单匹配算法:\n");
printf(" t在s中的位置=%d\n",Index(s,t));
GetNext(t,next); //由模式串t求出next值
GetNextval(t,nextval); //由模式串t求出nextval值
printf(" j ");
for (j=0;j<t.length;j++)
printf("%4d",j);
printf("\n");
printf(" t[j] ");
for (j=0;j<t.length;j++)
printf("%4c",t.data[j]);
printf("\n");
printf(" next ");
for (j=0;j<t.length;j++)
printf("%4d",next[j]);
printf("\n");
printf(" nextval");
for (j=0;j<t.length;j++)
printf("%4d",nextval[j]);
printf("\n");
printf("KMP算法:\n");
printf(" t在s中的位置=%d\n",KMPIndex(s,t));
printf("改进的KMP算法:\n");
printf(" t在s中的位置=%d\n",KMPIndex1(s,t));
return 0;
}
“`