//-----头文件------
#include<stdio.h>
#include<stdlib.h>
//------宏定义------
#define MAXSTRLEN 255
#define OVERFLOW -2
#define TURE 1
#define ERROR 0
//-------类型名替换--------
typedef unsigned char SString[MAXSTRLEN+1];
typedef int Bool;
//-----函数列表-----
Bool StrAssign(SString *T,char *s);
Bool StrCopy(SString *T,SString S);
Bool StrEmpty(SString S);
int StrCompare(SString S,SString T);
int StrLength(SString S);
Bool ClearString(SString *S);
Bool Concat(SString *T,SString S1,SString S2);
Bool SubString(SString *Sub,SString S,int pos,int len);
Bool StrInsert(SString *S,int pos,SString T);
Bool StrDelete(SString *S,int pos,int len);
Bool Replace(SString *S,SString T,SString V);
Bool DestroyString(SString *S);
int Index(SString S,SString T,int pos);
void StrOutput(SString S);
//-----主函数做替换测试------
void main()
{
SString t,*T=&t,s,*S=&s,s0,*S0=&s0;
int n;
StrAssign(T,"de");
StrAssign(S0,"ed");
StrAssign(S,"adegddefdef");
Replace(S,t,s0);
//printf("%d",Index(s,t,1));
//ClearString(S);
//StrCopy(S,t);
//for(n=1;n<=(*S)[0];n++)
StrOutput(s);
getchar();
getchar();
}
//------生成一个其值等于字符串常量的串T------
Bool StrAssign(SString *T,char *s)
{
int i;
for(i=0;'\0'!=*s;s++,i++)
{
(*T)[i+1]=*s;
}
(*T)[0]=i;
return TURE;
}
//------生成一个其值等于串S的串T------
Bool StrCopy(SString *T,SString S)
{
int i;
for(i=1;i<=S[0];i++)
{
(*T)[i]=S[i];
}
(*T)[0]=S[0];
return TURE;
}
//-----判断是否为空串------
Bool StrEmpty(SString S)
{
return S[0]==0?TURE:ERROR;
}
//-----比较两个串的大小------
int StrCompare(SString S,SString T)
{
int i;
if(0==S[0]||0==T[0])
{
return ERROR;
}
for(i=1;i<=S[0]&&i<=T[0];i++)
{
if(S[i]>T[i])
{
return 1;
}
else if(S[i]<T[i])
{
return -1;
}
else
{
continue;
}
}
if(S[0]==T[0])
{
return 0;
}
else if(S[0]>T[0])
{
return 1;
}
else
{
return -1;
}
}
//-----返回串的长度-----
int StrLength(SString S)
{
return S[0];
}
//-----清空串-----
Bool ClearString(SString *S)
{
if(0!=(*S)[0])
{
(*S)[1]='\0';
(*S)[0]=0;
}
return TURE;
}
//-----用T返回由S1和S2连接而成的串-----
Bool Concat(SString *T,SString S1,SString S2)
{
int i,j;
if(S1[0]+S2[0]<=MAXSTRLEN)
{
for(i=1;i<=S1[0];i++)
{
(*T)[i]=S1[i];
}
for(j=1;i<=S1[0]+S2[0];i++)
{
(*T)[i]=S2[j++];
}
(*T)[0]=S1[0]+S2[0];
return TURE;
}
else if(S1[0]<MAXSTRLEN&&(S1[0]+S2[0])>MAXSTRLEN)
{
for(i=1;i<=S1[0];i++)
{
(*T)[i]=S1[i];
}
for(j=1;i<=MAXSTRLEN;i++)
{
(*T)[i]=S2[j++];
}
(*T)[0]=MAXSTRLEN;
return ERROR;
}
else
{
for(i=1;i<=MAXSTRLEN;i++)
{
(*T)[i]=S1[i];
}
(*T)[0]=MAXSTRLEN;
return ERROR;
}
}
//-----输出串-----
void StrOutput(SString S)
{
int i;
for(i=1;i<=S[0];i++)
{
printf("%c",S[i]);
}
}
//-----用Sub返回串S中在pos位置开始长度为len的字串-----
Bool SubString(SString *Sub,SString S,int pos,int len)
{
int i,j;
if((pos>=1&&pos<=S[0])&&(len>=0&&len<=S[0]-pos+1))
{
for(i=1,j=pos;i<=len;i++)
{
(*Sub)[i]=S[j++];
}
(*Sub)[0]=len;
return TURE;
}
return ERROR;
}
//-----查找在相同子串的固定位置-----
int Index(SString S,SString T,int pos)
{
int m,n,i;
SString sub;
if(pos>0)
{
n=StrLength(S);
m=StrLength(T);
i=pos;
while(i<=n-m+1)
{
SubString(&sub,S,i,m);
if(0!=StrCompare(sub,T))
{
++i;
}
else
{
return i;
}
}
}
return 0;
}
//-----用V替换在S中存在的所有与T相同的子串-----
Bool Replace(SString *S,SString T,SString V)
{
int i=1,j=1,m=StrLength(T),n=StrLength(V);
while(i<=(*S)[0])
{
j=Index(*S,T,i);
StrDelete(S,j,m);
StrInsert(S,j,V);
i+=n+1;
}
return TURE;
}
//-----在串S的第pos位置前插入串T-----
Bool StrInsert(SString *S,int pos,SString T)
{
int m,n=StrLength(T),i=1,j=pos;
if(pos>=1&&pos<=StrLength(*S)+1)
{
while(i<=n)
{
for(m=StrLength(*S)+1;j<m;m--)
{
(*S)[m]=(*S)[m-1];
}
(*S)[j]=T[i++];
(*S)[0]+=1;
j+=1;
}
return TURE;
}
return ERROR;
}
//------删除串S中从pos位置起长度为len的子串------
Bool StrDelete(SString *S,int pos,int len)
{
int i=1,j,m,n;
if(pos>=1&&pos<=StrLength(*S)-len+1)
{
while(i<=len)
{
for(m=StrLength(*S),j=pos;j<=m;j++)
{
(*S)[j]=(*S)[j+1];
}
(*S)[0]-=1;
i++;
}
return TURE;
}
return ERROR;
}
//-----销毁串-----
Bool DestroyString(SString *S)
{
free(S);
return TURE;
}