BJFU_数据结构习题_259字符串的插入

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43722827/article/details/102755833

259字符串的插入

描述
编写算法,实现下面函数的功能。函数void insert(chars,chart,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

#include<iostream>
#include<cstring>
using namespace std;
#define MAX 100
void Insert(char *a,char *b,int pos)
{
	int i,j,m=0,f=0;
	int length=strlen(a)+strlen(b);
	for(j=0;j<strlen(a);j++)
	{
		f++;
		m++;
		if(m==pos)
		{
			for(i=0;i<strlen(b);i++)
			{
				f++;
				cout<<b[i];
			}
		}
		cout<<a[j];
		if(f==length)
			cout<<endl;
	}
}
int main()
{
	int pos;
	while(cin>>pos&&pos!=0)
	{
		char *a=new char[2*MAX];
		char *b=new char[MAX];
		cin>>a>>b;
		Insert(a,b,pos);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43722827/article/details/102755833
今日推荐