#include<iostream>
using namespace std;
#include<stdlib.h>
#define MAXSTRLEN 100
typedef struct{
char ch[MAXSTRLEN];
int length;
}HString;
//串S1和串S2连接成串S
char Concat(char *S1,char *S2,int s1_len,int s2_len)
{ HString S;
S.length=s1_len+s2_len;
int a,b=0,i=0;
for(i;i<s1_len;i++,S1++) //将串S1赋值给串S
S.ch[i]=*S1;
for(a=S.length-s2_len;a<S.length;a++,S2++) //将串S2连接在串S1的后面
S.ch[a]=*S2;
cout<<"\n串S1和串S2连接成串S :";
for(b;b<S.length;b++) //输出连接后的串S
cout<<S.ch[b];
return 0;}
//返回子串在主串中的位置
int Index(HString &S,HString &T)
{ int i=0;
int j=0;
while (i<S.length && j<T.length){
if (S.ch[i] == T.ch[j]){
i++;
j++;
}
else{
i=i-j+1;
j=0;
}
}
if (j>T.length-1)
return i-T.length;
else
return 0;
}
//输出串
int Show(HString &S)
{
for(int i=0; i<S.length; i++)
cout<<S.ch[i];
return 0;
}
//删除S串中与T串相等的串
void detele(){
HString S,T;
cout<<"请输入串S的长度:";
cin>>S.length;
cout<<"请输入串S:" ;
for(int i=0;i<S.length;i++)
cin>>S.ch[i];
cout<<"请输入需要删除串的长度:";
cin>>T.length;
cout<<"\n请输入需要删除的串:";
for(int j=0; j<T.length; j++)
cin>>T.ch[j];
cout<<endl;
int pos = Index(S,T);
for(int i=pos+T.length; i<S.length; i++)
S.ch[i-T.length] =S.ch[i];
S.length-=T.length;
cout<<"删除后串S为:";
Show(S);
}
//用V串替换S串中的T串
int Replace(HString &S,HString &T)
{HString V;
V.length = T.length;
cout<<"输入需要替换的串V:";
for(int i=0; i<V.length; i++)
cin>>V.ch[i];
int pos = Index(S,T);
for(int k=0; k<V.length; k++)
{S.ch[pos] = V.ch[k];
pos++;}
Show(S);
return 0;
}
int main()
{ HString S;
HString T;
char S1[MAXSTRLEN],S2[MAXSTRLEN];
int i=0; int s1_len,s2_len;
cout<<"请输入串S1的长度:";
cin>>s1_len;
cout<<"请输入串S1:";
for(i;i<s1_len;i++)
cin>>S1[i];
cout<<"请输入串S2的长度:";
cin>>s2_len;
cout<<"请输入串S2:";
for(i;i<s2_len;i++)
cin>>S2[i];
Concat( S1,S2,s1_len,s2_len);
cout<<"请输入被替换串T的长度:\n";
cin>>T.length;
cout<<"请输入串T:";
for(int t=0; t<T.length; t++)
cin>>T.ch[t];
Replace(S,T);
detele();
return 0;
}
#include<iostream>
using namespace std;
#include<stdlib.h>
#define MAXSTRLEN 100
typedef struct{
char ch[MAXSTRLEN];
int length;
}HString;
//串S1和串S2连接成串S
HString Concat(char *S1,char *S2,int s1_len,int s2_len)//???????????????????????函数类型换成HString
{ HString S;
S.length=s1_len+s2_len;
int a,b=0,i=0;
for(i;i<s1_len;i++,S1++) //将串S1赋值给串S
S.ch[i]=*S1;
for(a=S.length-s2_len;a<S.length;a++,S2++) //将串S2连接在串S1的后面
S.ch[a]=*S2;
cout<<"\n串S1和串S2连接成串S :";
for(b;b<S.length;b++) //输出连接后的串S
cout<<S.ch[b];
return S; //???????????????????????????????返回S
}
//返回子串在主串中的位置
int Index(HString &S,HString &T)
{ int i=0;
int j=0;
while (i<S.length && j<T.length){
if (S.ch[i] == T.ch[j]){
i++;
j++;
}
else{
i=i-j+1;
j=0;
}
}
if (j>T.length-1)
return i-T.length;
else
return 0;
}
//输出串
int Show(HString &S)
{
for(int i=0; i<S.length; i++)
cout<<S.ch[i];
return 0;
}
//删除S串中与T串相等的串
void detele(){
HString S,T;
cout<<"请输入串S的长度:";
cin>>S.length;
cout<<"请输入串S:" ;
for(int i=0;i<S.length;i++)
cin>>S.ch[i];
cout<<"请输入需要删除串的长度:";
cin>>T.length;
cout<<"\n请输入需要删除的串:";
for(int j=0; j<T.length; j++)
cin>>T.ch[j];
cout<<endl;
int pos = Index(S,T);
for(int i=pos+T.length; i<S.length; i++)
S.ch[i-T.length] =S.ch[i];
S.length-=T.length;
cout<<"删除后串S为:";
Show(S);
}
//用V串替换S串中的T串
int Replace(HString &S,HString &T)
{ HString V;
V.length = T.length;
cout<<"输入需要替换的串V:";
for(int i=0; i<V.length; i++)
cin>>V.ch[i];
int pos = Index(S,T);
for(int k=0; k<V.length; k++)
{
S.ch[pos] = V.ch[k];
pos++;
}
Show(S);
return 0;
}
int main()
{ HString S;
HString T;
char S1[MAXSTRLEN],S2[MAXSTRLEN];
int i=0; int s1_len,s2_len;
cout<<"请输入串S1的长度:";
cin>>s1_len;
cout<<"请输入串S1:";
for(i;i<s1_len;i++)
cin>>S1[i];
cout<<"请输入串S2的长度:";
cin>>s2_len;
cout<<"请输入串S2:";
for(i=0;i<s2_len;i++) //??????????????????????????????i=0
cin>>S2[i];
S = Concat( S1,S2,s1_len,s2_len); //????????????????????用S作为链接后的串
cout<<"请输入被替换串T的长度:\n";
cin>>T.length;
cout<<"请输入串T:";
for(int t=0; t<T.length; t++)
cin>>T.ch[t];
Replace(S,T); //?????????????????????????S从来没用过,为空,不能替换
detele();
return 0;
}
也许对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html