华为机试题目---字符串替换

题目要求:输入一个字符串,然后在输入一个整数,就是替换字符串的次数,然后依次输入需要替换的字符串……

例如:

输入: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;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值