蓝桥杯 字符串合并 C++算法训练 HERODING的蓝桥杯之路

资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
  输入两个字符串,将其合并为一个字符串后输出。
输入格式
  输入两个字符串
输出格式
  输出合并后的字符串
样例输入
一个满足题目要求的输入范例。
Hello

World
样例输出
HelloWorld
数据规模和约定
  输入的字符串长度0<n<100

解题思路:
这里我将提供三种方法解决这道题目。
1.利用string字符串的特性,加起来就可以拼接。
2.利用char型字符串的拼接函数strcat也可以实现拼接。
3.自己手动构建拼接函数也可以实现。
代码如下:

#include<bits/stdc++.h>

using namespace std;

int main(){
	string s1, s2;
	cin >> s1 >> s2;
	string s3 = s1 + s2;
	cout << s3;
	return 0;
}
#include<bits/stdc++.h>

using namespace std;

int main(){
	char s1[200], s2[100];
	cin >> s1 >> s2;
	strcat(s1, s2);
	cout << s1;
	return 0;
}
#include<bits/stdc++.h>

using namespace std;

void inmerage(char * s1, char * s2){
	int length = strlen(s1);
	int j = 0;
	for(int i = length; i <= length + strlen(s2); i ++){
		s1[i] = s2[j ++];
	}
}

int main(){
	char s1[200], s2[100];
	cin >> s1 >> s2;
	inmerage(s1, s2);
	cout << s1;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/HERODING23/article/details/106369881