#include <iostream>
using namespace std;
#define MAX 100
typedef struct
{
int length;
char str [MAX];
}sstring;
void In(sstring &sa)//初始化
{
char temp;
sa.length=0;
int i=0;
temp=getchar();
while ((temp!='\n'&&i<MAX))
{
sa.length++;
sa.str[i]=temp;
i++;
temp=getchar();
}
//cout<<"ddw";
}
sstring ToTwo(sstring & sb,sstring & sc)//合并两个串,返回合成后的串
{
sstring sa;
if (sb.length+sc.length>MAX)
{
cout<<"错误(数组越界)"<<endl;
//return sb;
}
else
{
sa.length=sb.length+sc.length;
for (int i=0;i<sb.length;i++)
{
sa.str[i]=sb.str[i];
}
for (int j=0;j<sc.length;j++)
{
sa.str[sb.length+j]=sc.str[j];
}
}
return sa;
}
sstring SubString(sstring *sa,int i,int j)//截取字串,从i到j,返回字串
{
sstring sc;
if (j>sa->length-1||i>sa->length-1)
{
printf("错误,越界/n");
}
else
{
sc.length=j-i+1;
for (int k=0;i!=j+1;k++,i++)
{
sc.str[k]=sa->str[i-1];
}
}
return sc;
}
sstring *Delete(sstring * sa,sstring * sc)//从sa中删除一个字串sc,并返回新的sa
{
int i=0;
while(i<MAX)
{
if (sa->str[i]==sc->str[0])
{
//cout<<"sqs"<<endl;
int k=i+1;
for (int j=1;j<sc->length;j++,k++)
{
if (sa->str[k]!=sc->str[j])
{
cout<<"二轮匹配失败"<<endl;
break;
}
//else
//k++;
}
if (k-i ==sc->length)
{
//cout<<"删除操作"<<endl;
int l=i;
//cout<<k<<endl;
//cout<<i<<endl;
for (int m=k;m<MAX;m++,l++)
{
//cout<<"jin";
sa->str[l]=sa->str[m];
if (m==MAX-1)
{
sa->length=sa->length-sc->length;
cout<<"success!"<<endl;
return sa;
}
}
}
}
else
{
//cout<<"else"<<endl;
i++;
}
}
cout<<"fail!"<<endl;
return sa;
}
int main()
{
cout<<"在屏幕上依次输入两个串"<<endl;
sstring sa;
sstring sb;
//初始化
In(sa);
In( sb);
cout<<"下面将这两个穿合并成一个串"<<endl;
sstring sd=ToTwo(sa,sb);
for (int i=0;i<sd.length;i++)
{
cout<<sd.str[i];
}
cout<<endl;
sstring se;
cout<<"输入从该串中要删除的部分"<<endl;
In( se);
sstring * sr=&se;
sstring *st=&sd;
sstring *sq=Delete(st,sr);
for (int i=0;i<sq->length;i++)//为什么可以直接对指针进行操作?
{
cout<<sq->str[i];
}
cout<<endl;
cout<<"输入要从串中截取的初位置,尾位置"<<endl;
int a,b;
cin>>a;
cin>>b;
sstring sf=SubString(st,a,b);
cout<<"截出的字串为"<<endl;
for (int i=0;i<sf.length;i++)
{
cout<<sf.str[i];
}
cout<<endl;
return 0;
}