标签:
//C语言串的顺序存储表示
//串的堆分配存储表示
//杨鑫
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSTRLEN 255
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
//定义数据元素结的构
typedef int Status;
typedef struct
{
char *ch;
int length;
}HString;
//生成一个其值等于串常量chars的串T
Status StrAssign(HString *T,char *chars)
{
int i,j;
if((*T).ch)
free((*T).ch);
i=strlen(chars);
if(!i)
{
(*T).ch=NULL;
(*T).length=0;
}
else
{
(*T).ch=(char*)malloc(i*sizeof(char));
if(!(*T).ch)
exit(OVERFLOW);
for(j=0;j<i;j++)
(*T).ch[j]=chars[j];
(*T).length=i;
}
return OK;
}
Status StrCopy(HString *T,HString S)
{
int i;
if((*T).ch)
free((*T).ch);
(*T).ch=(char*)malloc(S.length*sizeof(char));
if(!(*T).ch)
exit(OVERFLOW);
for(i=0;i<S.length;i++)
(*T).ch[i]=S.ch[i];
(*T).length=S.length;
return OK;
}
Status StrEmpty(HString S)
{
if(S.length==0&&S.ch==NULL)
return TRUE;
else
return FALSE;
}
int StrCompare(HString S,HString T)
{
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 S)
{
return S.length;
}
Status ClearString(HString *S)
{
if((*S).ch)
{
free((*S).ch);
(*S).ch=NULL;
}
(*S).length=0;
return OK;
}
Status Concat(HString *T,HString S1,HString S2)
{
int i;
if((*T).ch)
free((*T).ch);
(*T).length=S1.length+S2.length;
(*T).ch=(char *)malloc((*T).length*sizeof(char));
if(!(*T).ch)
exit(OVERFLOW);
for(i=0;i<S1.length;i++)
(*T).ch[i]=S1.ch[i];
for(i=0;i<S2.length;i++)
(*T).ch[S1.length+i]=S2.ch[i];
return OK;
}
Status SubString(HString *Sub, HString S,int pos,int len)
{
int i;
if(pos<1||pos>S.length||len<0||len>S.length-pos+1)
return ERROR;
if((*Sub).ch)
free((*Sub).ch);
if(!len)
{
(*Sub).ch=NULL;
(*Sub).length=0;
}
else
{
(*Sub).ch=(char*)malloc(len*sizeof(char));
if(!(*Sub).ch)
exit(OVERFLOW);
for(i=0;i<=len-1;i++)
(*Sub).ch[i]=S.ch[pos-1+i];
(*Sub).length=len;
}
return OK;
}
void InitString(HString *T)
{
(*T).length=0;
(*T).ch=NULL;
}
int Index(HString S,HString T,int pos)
{
int n,m,i;
HString sub;
InitString(&sub);
if(pos>0)
{
n=StrLength(S);
m=StrLength(T);
i=pos;
while(i<=n-m+1)
{
SubString(&sub,S,i,m);
if(StrCompare(sub,T)!=0)
++i;
else
return i;
}
}
return 0;
}
Status StrInsert(HString *S,int pos,HString T)
{
int i;
if(pos<1||pos>(*S).length+1)
return ERROR;
if(T.length)
{
(*S).ch=(char*)realloc((*S).ch,((*S).length+T.length)*sizeof(char));
if(!(*S).ch)
exit(OVERFLOW);
for(i=(*S).length-1;i>=pos-1;--i)
(*S).ch[i+T.length]=(*S).ch[i];
for(i=0;i<T.length;i++)
(*S).ch[pos-1+i]=T.ch[i];
(*S).length+=T.length;
}
return OK;
}
Status StrDelete(HString *S,int pos,int len)
{
int i;
if((*S).length<pos+len-1)
exit(ERROR);
for(i=pos-1;i<=(*S).length-len;i++)
(*S).ch[i]=(*S).ch[i+len];
(*S).length-=len;
(*S).ch=(char*)realloc((*S).ch,(*S).length*sizeof(char));
return OK;
}
Status Replace(HString *S,HString T,HString V)
{
int i=1;
if(StrEmpty(T))
return ERROR;
do
{
i=Index(*S,T,i);
if(i)
{
StrDelete(S,i,StrLength(T));
StrInsert(S,i,V);
i+=StrLength(V);
}
}while(i);
return OK;
}
void StrPrint(HString T)
{
int i;
for(i=0;i<T.length;i++)
printf("%c",T.ch[i]);
printf("\n");
}
int main()
{
int i;
char c,*p="God bye!",*q="God luck!";
HString t,s,r;
InitString(&t);
InitString(&s);
InitString(&r);
StrAssign(&t,p);
printf("定义了两个串:\n");
printf("串t为: ");
StrPrint(t);
printf("串长为 : %d 串空否?%d ( 1 : 空 0 : 否)\n", StrLength(t), StrEmpty(t));
StrAssign(&s,q);
printf("串s为: ");
StrPrint(s);
i=StrCompare(s,t);
if(i<0)
c='<';
else if(i==0)
c='=';
else
c='>';
printf("串s%c串t\n",c);
Concat(&r,t,s);
printf("串t联接串s产生的串r为: ");
StrPrint(r);
StrAssign(&s,"oo");
printf("串s为: ");
StrPrint(s);
StrAssign(&t,"o");
printf("串t为: ");
StrPrint(t);
Replace(&r,t,s);
printf("把串r中和串t相同的子串用串s代替后,串r为:\n");
StrPrint(r);
ClearString(&s);
printf("串s清空后,串长为 : %d 是否为空?%d ( 1 : 空 0 : 否)\n",StrLength(s),StrEmpty(s));
SubString(&s,r,6,4);
printf("串s为从串r的第6个字符起的4个字符,长度为 %d 串s为: ",s.length);
StrPrint(s);
StrCopy(&t,r);
printf("复制串t为串r,串t为: ");
StrPrint(t);
StrInsert(&t,6,s);
printf("在串t的第6个字符前插入串s后,串t为: ");
StrPrint(t);
StrDelete(&t,1,5);
printf("从串t的第1个字符起删除5个字符后,串t为: ");
StrPrint(t);
printf("%d是从串t的第1个字符起,和串s相同的第1个子串的位置\n",Index(t,s,1));
printf("%d是从串t的第2个字符起,和串s相同的第1个子串的位置\n",Index(t,s,2));
return 0;
}
标签:
原文地址:http://blog.csdn.net/u012965373/article/details/45954953