#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef int Status;
#define TRUE 1;
#define FALSE 0;
#define OK 1;
#define ERROR -1;
#define INFEASBIOLE -2;
#define OVERFLOW 0;
//串的存储结构
typedef struct
{
char *ch;
int length;
} HString;
//串的基本操作
Status StrAssign(HString *T,char *chars)
{
//生成一个其值等于chars的串T
int i;
char *ch;
if(T->ch)
free(T->ch);
for(i=0,ch=chars; *ch; i++,ch++);
if(!i)
{
T->ch=NULL;
T->length=0;
}
else
{
T->ch=(char*)malloc(sizeof(char)*i);
if(!T->ch)
return OVERFLOW;
for(i=0,ch=chars; *ch; i++,ch++)
T->ch[i]=*ch;
T->ch[i]='\0';
T.length=i;
}
return OK;
}
Status StrCopy(HString &T,Hstring S)
{
//由串S复制得串T
int i;
char *ch;
if(T.ch)
free(T.ch);
for(i=0,ch=S; *ch; i++,ch++);
T.length=i;
T.ch=(char*)malloc(sizeof(char)*i);
if(!i)
T.ch=NULL;
for(i=0,ch=S; *ch; i++,ch++)
T.ch[i]=*ch;
T.ch[i]='\0';
return OK;
}
Status StrEmpty(HString T)
{
//判断串T是否为空
return (T.ch==NULL);
}
Status StrCompare(HString S,HString T)
{
//若S>T,返回>0
//若S<T,返回<0
//若S==T,返回0
int i;
for(i=0; i<S.length&&i<T.length; i++)
if(S.ch[i]!=T.ch[i])
return (S.ch[i]-T.ch[i]);
return (S.length-T.length);
}
int StrLength(HString T)
{
//返回T的长度
return T.length;
}
Status ClearString(HString &T)
{
//将S置为空串
if(T.ch)
{
free(T.ch);
T.ch=NULL;
}
T.length=0;
return OK;
}
Status Concat(HString &T,HString S1,HString S2)
{
//用T返回由S1和S2连接而成的新串
char *ch;
int i;
if(T.ch)
free(T.ch);
T.ch=(char*)malloc(sizeof(char)*(S1.length+S2.length));
if(!T.ch)
return OVERFLOW;
for(i=0,ch=S1; *ch; i++,ch++)
T.ch[i]=*ch;
for(ch=S2; *ch; i++,ch++)
T.ch[i]=*ch;
T.ch[i]='\0';
return OK;
}
Status SubString(HString &Sub,HString S,int pos,int len)
{
//用SUB返回串S从第pos个字符起长度为len的子串
int i;
char *ch;
if(pos<1||pos>S.length||len<0||len>S.length-pos+1)
return ERROR;
if(Sub.ch)
free(Sub.ch);
Sub.ch=(char*)malloc(sizeof*(char)*len);
Sub.length=0;
if(!Sub.ch)
return OVERFLOW;
if(!len)
Sub.ch=NULL;
for(i=0,ch=S[pos]; i<len; i++,ch++)
Sub.ch[i]=*ch;
Sub.ch[i]='\0';
return OK;
}
Status Index(HString S,HString T,int pos)
{
//若主串S中存在和串T值相同的子串,则返回它在主串中第pos个字符之后第一次出现的位置
//否则函数值为0
int i,j;
i=pos-1;
j=0;
while(i<=S.length&&j<=T.length)
{
if(S[i]==T[j])
{
i++;
j++;
}
else
{
i=i-j+1;
j=0;
}
}
if(j>T.length)
return i-T.length;
return 0;
}
Status Index_KMP(HString S,HString T,int pos)
{
//若主串S中存在和串T值相同的子串,则返回它在主串中第pos个字符之后第一次出现的位置
//否则函数值为0
//KMP算法
int i=pos-1;
int j=1;
int len=StrLength(T);
int next[len];
get_next(T,*next);
while(i<=S.length&&j<=T.length)
{
if(j==0||S[i]==T[j])
{
i++;
j++;
}
else
j=next[j];
}
if(j>T.length)
return i-T.length;
return ERROR;
}
int get_next(HString T,int next[])
{
//求next数组
int i,j;
next[0]=0;
for(i=1; i<T.length; i++)
{
j=next[i-1];
while(j!=0&&T.ch[i]!=T.ch[j])
j=next[j-1];
if(T.ch[i]==T.ch[j])
next[i]=j+1;
else
next[i]=0;
}
return 0;
}
Status Replace(HString &S,HString T,HString V)
{
//用V替换主串S中出现的所有与T相等的不重叠的子串
int i=1;
if(StrEmpty(S))
return ERROR;
while(i<S.length)
{
i=Index_KMP(S,T,i);
if(i)
{
StrDelete(&S,i,T.length);
StrInsert(&S,i,V.length);
i+=V.length;
}
}
return OK;
}
Status StrInsert(HString &S,int pos,HString T)
{
//在串的第pos个字符之前插入串T
S.length+=T.length;
S.ch=(char*)malloc(sizeof(char)*S.length);
int i;
char *ch;
for(i=0,ch=S.ch[i]; i<pos; i++)
S.ch[i+pos]=*ch;
S.ch[i+pos]='\0';
for(i=pos,ch=T.ch[i]; *ch; i++,ch++)
S.ch[pos]=*ch;
return OK;
}
Status StrDelete(HString &T,int pos,int len)
{
//在主串S中删除自pos位置起长度为len的子串
if(pos<0||pos>T.length||len>T.length||pos+len>T.length)
return ERROR;
HString S;
S.length=T.length-len;
S.ch=(char*)malloc(sizeof(char)*S.length);
char *ch;
int i;
for(i=0,ch=T.ch; i<pos; i++,ch++)
S.ch[i]=*ch;
for(ch=T.ch[i+len]; *ch; i++,ch++)
S.ch[i]=*ch;
S.ch[i]='\0';
free(T.ch);
T.length=T.length-len;
T.ch=(char*)malloc(sizeof(char)*S.length);
StrCopy(&T,S);
free(S.ch);
S.length=0;
return OK;
}
Status DestroyString(HString &S)
{
//串S被销毁
if(S.ch)
free(S.ch);
S.length=0;
return OK;
}
int main()
{
printf("Hello world!\n");
return 0;
}