题目要求:输入一个字符串,然后在输入一个整数,就是替换字符串的次数,然后依次输入需要替换的字符串……
例如:
输入:abcdefg
3
a->qwe
b->s
fg->abc
输出:qwescdeabc
//字符串替换
#include<iostream>
#include<vector>
#include<string>
#define max 100
using namespace std;
void change(char str[],int count,int len_str,string buff,int jilu,int count_re_str)
{
int j=0;
int p=0;
char q[max];
for(int i=0;i<len_str;i++)
{
q[i]=str[i];//防止后面str改变影响元数组
}
for(int i=jilu+count;i<len_str;i++)
{
str[jilu+count_re_str+p++]=q[i];
}
for(int i=jilu;i<jilu+count_re_str;i++)
{
str[i]=buff[j++];
}
str[jilu+count_re_str+p]='\0';
}
void replace_str(char str[],int len_str,vector<string> restr,int index)
{
bool flag=true;
bool anquan=true;
string s_str;
string buff;
s_str=restr[index];
int jilu;
int count=0;
int count_re_str=0;
int n=s_str.size();
for(int i=0;i<n;i++)
{
if(s_str[i]>='a'&&s_str[i]<='z'&&anquan)
{
for(int j=0;j<len_str&&flag;j++)
{
if(s_str[i]==str[j])
{
flag=false;
jilu=j;
}
}
count++;
}
else
{
if(anquan)
{
i=i+2;
anquan=false;
}
if(s_str[i]>='a'&&s_str[i]<='z')
{
buff.push_back(s_str[i]);
count_re_str++;
}
}
}
change(str,count,len_str,buff,jilu,count_re_str);
}
int main()
{
char str[max];
vector<string> restr;
string s;
gets(str);
int sum;
cout<<"请输入次数:";
cin>>sum;
for(int i=0;i<sum;i++)
{
cin>>s;
restr.push_back(s);
}
for(int i=0;i<sum;i++)
{
int len_str=strlen(str);
replace_str(str,len_str,restr,i);
}
int q=0;
while(str[q]!='\0')
{
cout<<str[q++];
}
cout<<endl;
system("pause");
return 0;
}