改程序,将题改为S T V可由我自己输入 enter后输出结果

来源:百度知道 编辑:UC知道 时间:2024/05/25 07:42:41
#include<iostream.h>
typedef struct
{
char *ch;
int length;
}Str;
void InitStr(Str &T,char *ch1)
{
int i;
char *c;
if(T.ch)
{
delete T.ch;T.ch = NULL;
}
for( i=0, c=ch1; c[i]!=NULL; i++ );
if(!i){T.ch = NULL; T.length =0;}
else
{
if(!(T.ch = (char*)new char[i]))
return;
for(int j=0;j<i;j++)
T.ch[j]=ch1[j];
T.length=i;
}
return;
}
int StrLength(Str S)
{
return S.length;
}
int StrCompare(Str S,Str T)
{
for(int 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;
}
void ClearStr(Str &S)
{
if(S.ch)
{
delete S.ch;S.ch=NULL;
}
S.length=0;
}
void Concat(Str &T,Str S1,Str S2)
{
if(T.ch )
delete T.ch ;
if(!(T.ch=(char*)new char[S1.

把主函数改成这样
void main()
{
Str S,T,V;
char *p;
p=new char[100];
S.ch=NULL;
T.ch=NULL;
V.ch=NULL;
int pos=2,len=3;
cout<<"串S,T和V存在,T是非空串" <<endl
<<"用V替换主串S中出现的所有与T相等的不重叠的子串"<<endl
<<"如 S=";
cin>>p;
InitStr(S,p);
cout<<"T=";
cin>>p;
InitStr(T,p);
cout<<"V=";
cin>>p;
InitStr(V,p);
Replace(S,T,V);
cout<<endl
<<"替换结果为:";
Display(S);
cout<<endl;
}

http://baike.360.cn/4024037/12769473.html?page=1&type=1#layer_4