串的堆分配表示

串的堆分配表示

主要还是两个函数,Index Replace

"a.h"

#include<stdio.h>
#include<string.h>
#include<malloc.h>

#define TRUE 1
#define OK 1
#define FALSE 0
#define ERROR 0

typedef int Status;
typedef struct{
     char *ch;
  int len;
}HString;             //堆分配存储表示

"b.h"

Status InitString(HString *T)
{
    (*T).ch=NULL;
 (*T).len=0;
 return OK;
}

Status StrAssign(HString *T,char *chars)
{

 int i;
    if((*T).ch)
    free((*T).ch);
 if(strlen(chars)==0){
   (*T).ch=NULL;
   (*T).len=0;
 }
 else{
  (*T).len=strlen(chars);
  (*T).ch=(char*)malloc((*T).len*sizeof(char));
  if(!(*T).ch)
   return ERROR;
  for(i=0;i<(*T).len;i++)
   (*T).ch[i]=chars[i];
 }
 return OK;
}

Status StrCopy(HString *T,HString S)
{
       int i;
    if((*T).ch)
     free((*T).ch);
    (*T).ch=(char*)malloc(S.len*sizeof(char));
        if(!(*T).ch)
   return ERROR;
    for(i=0;i<S.len;i++)
      (*T).ch[i]=S.ch[i];
    (*T).len=S.len;
    return OK;
}

Status StrLength(HString T)
{
    return T.len;
}

Status StrEmpty(HString S)
{
   if(S.len==0&&S.ch==NULL)
    return TRUE;
   else
    return FALSE;
}

Status StrClear(HString *T)
{
 if((*T).ch){
     free((*T).ch);
     (*T).ch=NULL;
 }
      (*T).len=0;
   return OK;
}

Status Concat(HString *T,HString S1,HString S2)  //用T返回s1和s2的链接
{
    int i;
       StrClear(T);
    (*T).ch=(char*)malloc((S1.len+S2.len)*sizeof(char));
    if(!(*T).ch)
   return ERROR;
    (*T).len=S1.len+S2.len;
    for(i=0;i<S1.len;i++)
     (*T).ch[i]=S1.ch[i];
    for(i=S1.len;i<(*T).len;i++)
     (*T).ch[i]=S2.ch[i-S1.len];
    return OK;
}

Status SubString(HString *Sub,HString S,int pos,int len)  // 用Sub返回S的从第pos个的len串
{
   int i;
      if(pos<0||len<1||pos+len-1>S.len)
    return ERROR;
   StrClear(Sub);
   (*Sub).ch=(char*)malloc(len*sizeof(char));
   if(!(*Sub).ch)
    return ERROR;
   (*Sub).len=len;
   for(i=0;i<len;i++)
    (*Sub).ch[i]=S.ch[pos+i-1];      //需减1,因为是在数组中
   return OK;
}
Status StrEqual(HString T,HString S)
{
 int i;
    if(T.len!=S.len)
  return ERROR;
     for(i=0;i<T.len;i++)
   if(T.ch[i]!=S.ch[i])
    return ERROR;
     return OK;
}
int Index(HString S,HString T,int pos)  //S存在和T相同的子串,返回他在S的第pos个后第一次出现的位置
{
 HString sub;
 int m,n,i;
 InitString(&sub);
 if(pos>0){
  n=StrLength(S);
  m=StrLength(T);
  i=pos;
  while(i<=n-m+1){
    SubString(&sub,S,i,T.len);
          if(StrEqual(sub,T))
     return i;
    i++;
  }
 }
  return 0;

}

Status StrInsert(HString *S,int pos,HString T)  //S中插入
{
       int i;
          if(pos<1||pos>(*S).len+1)
     return ERROR;
          if(T.len){
        (*S).ch=(char *)realloc((*S).ch,((*S).len+T.len)*sizeof(char));
     if(!(*S).ch)
      return ERROR;
     for(i=(*S).len-1;i>=pos-1;--i)
      (*S).ch[T.len+i]=(*S).ch[i];
     for(i=0;i<T.len;i++)
      (*S).ch[pos+i-1]=T.ch[i];
     (*S).len+=T.len;
    }
    return OK;
}

Status StrDelete(HString *S,int pos,int len)
{
    int i;
       if(pos<1||pos+len-1>(*S).len||len<0)
     return ERROR;
    for(i=pos-1;i<=(*S).len-len;i++)
     (*S).ch[i]=(*S).ch[i+len];
    (*S).len-=len;
    (*S).ch=(char *)realloc((*S).ch,((*S).len)*sizeof(char));
    return OK;

}

Status StrPrint(HString S)
{
     int i;
  for(i=0;i<S.len;i++)
   printf(" %c ",S.ch[i]);
  printf("\n");
  return OK;
}
Status Replace(HString *S,HString T,HString V)   //用V替换S中与T相同的串
{
    int i=1;
 if(StrEmpty(T))
  return ERROR;
 do{
  i=Index(*S,T,i);
  if(i){
     StrDelete(S,i,T.len);
     StrInsert(S,i,V);
     i+=T.len;
  }
 }while(i);
 return TRUE;
}


 

"main.h"

#include"a.h"
#include"b.h"
void main()
{
   HString S,T,L;
   char *ch="lijian";
   char *ch1="nuist";
   InitString(&S);
   InitString(&T);
   InitString(&L);
   StrAssign(&T,ch1);
   StrAssign(&S,ch);
   StrPrint(S);
   StrCopy(&S,T);
   StrPrint(S);
   printf("S的长度 %d\n",StrLength(S));
   Concat(&L,S,T);
   StrPrint(L);
   SubString(&S,L,3,5);
   StrPrint(S);
   printf("********%d\n",Index(L,S,1));
   StrInsert(&L,2,S);
   StrPrint(L);
   StrDelete(&L,2,5);
   StrPrint(L);
    printf("L的长度 %d\n",StrLength(L));
   StrPrint(S);
    printf("S的长度 %d\n",StrLength(S));
   StrPrint(T);
    printf("T的长度 %d\n",StrLength(T));
   Replace(&L,T,S);
   StrPrint(L);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值