字符串的插入

#include <iostream>
#include <string>
using namespace std;

void insert(string s,string str,int pos)
{
	string rear,front,newstr;
	front=s.substr(0,pos);
	rear=s.substr(pos,s.length()-pos);
	newstr=front+str+rear;
//	cout<<front<<endl;
//	cout<<str<<endl;
//	cout<<rear<<endl;
	cout<<newstr<<endl;
	return; 
}


int main()
{
	while(1)
	{
		string s,str;
		int pos;
		cin>>pos;
		if(0==pos)break;
		cin>>s>>str;
		insert(s,str,pos-1);
	 } 

	
}


描述

编写算法,实现下面函数的功能。函数void insert(char*s,char*t,int pos)将字符串t插入到字符串s中,插入位置为pos(插在第pos个字符前)。假设分配给字符串s的空间足够让字符串t插入。(说明:不得使用任何库函数)

输入

多组数据,每组数据有三行,第一行为插入的位置pos,第二行为要被插入的字符串s,第三行为待插入的字符串t。当pos为“0”时输入结束。

输出

对于每组数据输出一行,为t插入s后的字符串。

输入样例 1 

1
abcde
abc
2
acd
baaaa
0

输出样例 1

abcabcde
abaaaacd
发布了100 篇原创文章 · 获赞 4 · 访问量 3667

猜你喜欢

转载自blog.csdn.net/weixin_43673589/article/details/104420827