[求助]关于串堆分配的基本操作???
帮偶看下ReplaceStr函数,虽然可以达到效果...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char *ch;
int length;
}HString;
void temp(HString *t);
void HStrInit(HString *s)//初始
{
s->ch=NULL;
s->length=0;
}
int HStrLen(HString *s)//串长
{
return(s->length);
}
/*
int StrLen(char *s)
{
int len=0;
while(*s++!='\0')
{
len++;
}
return(len);
}
void StrCpy(char *d,char *s)
{
while(*d++=*s++);
}
*/
void StrAssign(HString *s,char *ts)//赋值
{
int len=strlen(ts);
if(len)
{
s->ch=(char *)malloc(len+1);//'\0'
strcpy(s->ch,ts);
s->length=len;
}
}
void StrCpy(HString *d,HString *s)//拷贝
{
int len=HStrLen(s);
if(len)
{
d->ch=(char *)malloc(len);
strcpy(d->ch,s->ch);
d->length=len;
}
}
void Concat(HString *s,HString *d)//连接
{
int n=0,len=d->length;
if(!len)
{
printf("ERROR d->length\n");
return;
}
s->ch=(char *)realloc(s->ch,s->length+len);
for(n=s->length;n<s->length+len;n++)
s->ch[n]=d->ch[n-s->length];
s->length+=d->length;
}
void subStr(HString *s,HString *sub,int pos,int len)//子串
{
if(pos<1 || pos>s->length+1 || len<0 || len+pos>s->length+1)
{
printf("ERROR: pos or len\n");
return;
}
if(!len)
{
sub->ch=NULL;
sub->length=0;
}
else
{
sub->ch=(char *)malloc(len+1);//'\0'
strncpy(sub->ch,s->ch+pos-1,len);
sub->ch[len]='\0';//not len+1
sub->length=len;
}
}
void InsertStr(HString *s,int pos,HString *i)//插入
{
int n=0,len=i->length;
if(pos<1 || pos>s->length){
printf("ERROR: pos\n");
return;
}
if(len)
{
s->ch=(char *)realloc(s->ch,s->length+len);
for(n=s->length;n>pos-1;n--)//pos-1
s->ch[n+len]=s->ch[n];//将pos之后的字符整体移动len长度
for(n=pos;n<pos+len;n++)//插入
s->ch[n]=i->ch[n-pos];
s->length+=len;
} else printf("ERROR: i->length\n");
}
int Index(HString *s,HString *d,int pos)//查找子串,返回第一个匹配位置
{
if(pos<1 || pos>s->length || d->length==0 || d->length>s->length)
{
printf("ERROR: pos || d->length\n");
return 0;
}
int t=pos;
while(t+d->length<=s->length+1)
{
HString T;
subStr(s,&T,t,d->length);
if(strcmp(T.ch,d->ch)==0)
return t;
t++;
}
return 0;
}
void DeleteStr(HString *s,int pos,int len)//删除
{
int n=0;
if(pos<1 || pos>s->length || len<=0 || pos+len>s->length)
{
printf("ERROR: pos || len\n");
return;
}
for(n=pos-1;n<pos-1+len;n++)
{
}
}
void ReplaceStr(HString *ms,HString *sub,HString *tar)//替换
{
if(sub->length==0 || tar->length==0)
{
printf("ERROR: sub->length || tar->length\n");
return;
}
int pos=1;
pos=Index(ms,sub,pos);
while(pos)
{
int n=0;
if(sub->length>=tar->length)
{
for(n=0;n<sub->length;n++)
{
ms->ch[pos+n-1]=tar->ch[n];
}
if(sub->length > tar->length)
{
for(n=pos-1+tar->length;n<=ms->length;n++)//前移sub->length-tar->length
ms->ch[n]=ms->ch[n+sub->length-tar->length];
}
}
else
{
ms->ch=(char *)realloc(ms->ch,ms->length+tar->length-sub->length);
for(n=ms->length+tar->length-sub->length;n>pos+sub->length-1;n--)//后移tar->length-sub->length
{
//printf("%d <--- %d %c \n",n,n-tar->length+sub->length,ms->ch[n-tar->length+sub->length]);
ms->ch[n]=ms->ch[n-tar->length+sub->length];
//if(ms->ch[n]=='\0') printf("found\n");
}
for(n=pos-1;n<pos+tar->length-1;n++)
ms->ch[n]=tar->ch[n-pos+1];
ms->length+=(tar->length-sub->length);
//temp(ms);//???第二次移动时,"丢失"了'\0'
ms->ch[ms->length]='\0';//-_-||
}
pos=Index(ms,sub,++pos);
}
}
void temp(HString *t)
{
int n=0;
while(*t->ch++!='\0')
n++;
printf("%d\n",n);
}
int main(void)
{
HString s,d,t;
HStrInit(&s);
HStrInit(&d);
HStrInit(&t);
char *t1="a123b123c";
char *t2="123";
char *t3="TNND";
StrAssign(&s,t1);
StrAssign(&d,t2);
StrAssign(&t,t3);
//StrCpy(&d,&s);
//InsertStr(&s,9,&d);
//Concat(&s,&d);
//printf("%s %d\n",s.ch,s.length);
//subStr(&s,&d,8,3);
//printf("%d\n",Index(&s,&d,1));
ReplaceStr(&s,&d,&t);
printf("%s\n",s.ch);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char *ch;
int length;
}HString;
void temp(HString *t);
void HStrInit(HString *s)//初始
{
s->ch=NULL;
s->length=0;
}
int HStrLen(HString *s)//串长
{
return(s->length);
}
/*
int StrLen(char *s)
{
int len=0;
while(*s++!='\0')
{
len++;
}
return(len);
}
void StrCpy(char *d,char *s)
{
while(*d++=*s++);
}
*/
void StrAssign(HString *s,char *ts)//赋值
{
int len=strlen(ts);
if(len)
{
s->ch=(char *)malloc(len+1);//'\0'
strcpy(s->ch,ts);
s->length=len;
}
}
void StrCpy(HString *d,HString *s)//拷贝
{
int len=HStrLen(s);
if(len)
{
d->ch=(char *)malloc(len);
strcpy(d->ch,s->ch);
d->length=len;
}
}
void Concat(HString *s,HString *d)//连接
{
int n=0,len=d->length;
if(!len)
{
printf("ERROR d->length\n");
return;
}
s->ch=(char *)realloc(s->ch,s->length+len);
for(n=s->length;n<s->length+len;n++)
s->ch[n]=d->ch[n-s->length];
s->length+=d->length;
}
void subStr(HString *s,HString *sub,int pos,int len)//子串
{
if(pos<1 || pos>s->length+1 || len<0 || len+pos>s->length+1)
{
printf("ERROR: pos or len\n");
return;
}
if(!len)
{
sub->ch=NULL;
sub->length=0;
}
else
{
sub->ch=(char *)malloc(len+1);//'\0'
strncpy(sub->ch,s->ch+pos-1,len);
sub->ch[len]='\0';//not len+1
sub->length=len;
}
}
void InsertStr(HString *s,int pos,HString *i)//插入
{
int n=0,len=i->length;
if(pos<1 || pos>s->length){
printf("ERROR: pos\n");
return;
}
if(len)
{
s->ch=(char *)realloc(s->ch,s->length+len);
for(n=s->length;n>pos-1;n--)//pos-1
s->ch[n+len]=s->ch[n];//将pos之后的字符整体移动len长度
for(n=pos;n<pos+len;n++)//插入
s->ch[n]=i->ch[n-pos];
s->length+=len;
} else printf("ERROR: i->length\n");
}
int Index(HString *s,HString *d,int pos)//查找子串,返回第一个匹配位置
{
if(pos<1 || pos>s->length || d->length==0 || d->length>s->length)
{
printf("ERROR: pos || d->length\n");
return 0;
}
int t=pos;
while(t+d->length<=s->length+1)
{
HString T;
subStr(s,&T,t,d->length);
if(strcmp(T.ch,d->ch)==0)
return t;
t++;
}
return 0;
}
void DeleteStr(HString *s,int pos,int len)//删除
{
int n=0;
if(pos<1 || pos>s->length || len<=0 || pos+len>s->length)
{
printf("ERROR: pos || len\n");
return;
}
for(n=pos-1;n<pos-1+len;n++)
{
}
}
void ReplaceStr(HString *ms,HString *sub,HString *tar)//替换
{
if(sub->length==0 || tar->length==0)
{
printf("ERROR: sub->length || tar->length\n");
return;
}
int pos=1;
pos=Index(ms,sub,pos);
while(pos)
{
int n=0;
if(sub->length>=tar->length)
{
for(n=0;n<sub->length;n++)
{
ms->ch[pos+n-1]=tar->ch[n];
}
if(sub->length > tar->length)
{
for(n=pos-1+tar->length;n<=ms->length;n++)//前移sub->length-tar->length
ms->ch[n]=ms->ch[n+sub->length-tar->length];
}
}
else
{
ms->ch=(char *)realloc(ms->ch,ms->length+tar->length-sub->length);
for(n=ms->length+tar->length-sub->length;n>pos+sub->length-1;n--)//后移tar->length-sub->length
{
//printf("%d <--- %d %c \n",n,n-tar->length+sub->length,ms->ch[n-tar->length+sub->length]);
ms->ch[n]=ms->ch[n-tar->length+sub->length];
//if(ms->ch[n]=='\0') printf("found\n");
}
for(n=pos-1;n<pos+tar->length-1;n++)
ms->ch[n]=tar->ch[n-pos+1];
ms->length+=(tar->length-sub->length);
//temp(ms);//???第二次移动时,"丢失"了'\0'
ms->ch[ms->length]='\0';//-_-||
}
pos=Index(ms,sub,++pos);
}
}
void temp(HString *t)
{
int n=0;
while(*t->ch++!='\0')
n++;
printf("%d\n",n);
}
int main(void)
{
HString s,d,t;
HStrInit(&s);
HStrInit(&d);
HStrInit(&t);
char *t1="a123b123c";
char *t2="123";
char *t3="TNND";
StrAssign(&s,t1);
StrAssign(&d,t2);
StrAssign(&t,t3);
//StrCpy(&d,&s);
//InsertStr(&s,9,&d);
//Concat(&s,&d);
//printf("%s %d\n",s.ch,s.length);
//subStr(&s,&d,8,3);
//printf("%d\n",Index(&s,&d,1));
ReplaceStr(&s,&d,&t);
printf("%s\n",s.ch);
return 0;
}