首页 > 试题广场 >

合并符串

[编程题]合并符串
  • 热度指数:6252 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
给定两个字符串S1和S2,合并成一个新的字符串S。 合并规则为,S1的第一个字符为S的第一个字符,将S2的最后一个字符作为S的第二个字符; 将S1的第二个字符作为S的第三个字符,将S2的倒数第二个字符作为S的第四个字符,以此类推。

输入描述:
包含多组测试数据,每组测试数据包含两行,代表长度相等的两个字符串S1和S2(仅由小写字母组成,长度不超过100)。


输出描述:
合并后的新字符串S
示例1

输入

abc
def

输出

afbecd

python solution:

while True:
    try:
        a,b,res=input(),input()[::-1],""
        for i in range(len(a)):
            res=res+a[i]+b[i]
        print(res)
    except:
        break
发表于 2017-10-01 16:14:19 回复(1)
//先循环短字符串长度的次数,然后把长的字符串剩下的拼接上
#include <iostream>
#include <string>
using namespace std;
int main(){ 
    int i;
    string s1,s2;
    while(cin>>s1>>s2){
        int len1=s1.length();
        int len2=s2.length();             
        string str="";
        int len=len1>len2?len2:len1;
        for(i=0;i<len;i++){
            str+=s1[i];
            str+=s2[len2-i-1];
        }
        if(len1>len2){
            for(i=len2;i<len1;i++)
                str+=s1[i];
        }else{
            for(i=len1;i<len2;i++)
                str+=s2[i];
        }
        cout<<str<<endl;
    } 
    return 0; 
}

编辑于 2017-12-13 21:15:15 回复(0)
#include <iostream>
#include <string>
using namespace std;

int main() {
    string S1, S2;
    while (cin >> S1 >> S2) {
        int lenS1 = S1.length(), lenS = 2 * lenS1; //定义并计算S1, S2, S的长度
        for (int i = 0; i < lenS; i++)
            if (i % 2 == 0) //若循环变量为偶数,则打印S1的正序值
                cout << S1[i / 2]; 
            else //否则打印S2的逆序值
                cout << S2[(lenS - i) / 2];
        cout << endl;
    }
}

发表于 2017-02-13 10:08:54 回复(0)
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
vector<char> s;//存储合并后的字符串

void conslidate(string s1, string s2, int n)
{
    //s.clear();
    for (int i = 0; i < n; i++)
    {
        char a = s1[i];
        s.push_back(a);
       
        char b = s2[i];
        s.push_back(b);
    }
   
}

int main(void)
{
    string s1, s2;
    cin >> s1 >> s2;
    
    reverse(s2.begin(), s2.end());//转置第二个字符串
    int n = s1.length();
    conslidate(s1, s2, n);
    
    for (vector<char>::iterator it = s.begin(); it != s.end(); it++)
        cout << *it;
    
    return 0;
}

发表于 2021-02-24 16:46:22 回复(0)
提供一个栈的思路
#include<bits/stdc++.h>
using namespace std;
stack<char>s;
int main()
{
    char s1[101],s2[101];
    scanf("%s",s1);
    scanf("%s",s2);
    int j=0;
    while(s2[j]!='\0')
    {
       s.push(s2[j]);
        j++;
    }
    int i=0;
    while(j--)
    {
        printf("%c",s1[i]);
        if(!s.empty())
        {
            printf("%c",s.top());
            s.pop();
        }
        i++;
    }
    return 0;
}


发表于 2020-04-09 09:44:00 回复(0)
#include<stdio.h>//先复制a字符串再倒序复制b字符串即可,别忘记i+=2而不是i++
int main()
{
    char a[100],b[100],c[200];
    scanf("%s%s",a,b);
    int i,j,n1,n2,n3,num;
    n1=strlen(a);n2=strlen(b);n3=n1+n2;
    c[n3]='\0';//别忘了\0要不然后面乱码
    num=0;
    for(i=0;i<n3;i+=2)
        c[i]=a[num++];
    num=n2-1;
    for(i=1;i<n3;i+=2)
        c[i]=b[num--];
    printf("%s",c);
}

发表于 2020-04-07 16:11:02 回复(0)
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char[] a = scanner.next().toCharArray();
        char[] b = scanner.next().toCharArray();
        StringBuilder builder = new StringBuilder();
        int i= 0;
        int j= b.length-1;
        while (i<a.length||j>=0) builder.append(a[i++]).append(b[j--]);
        System.out.println(builder.toString());
    }
}


发表于 2020-03-14 16:23:14 回复(0)
#include<iostream>
#include<string>

using namespace std;

int main(){
    
    string str1;
    string str2;
    char str[200];
    while(getline(cin , str1)){
        getline(cin , str2);
        int len = str1.size();
        
        for(int i=0;i<str1.size();i++){
            str[2*i] = str1[i];
            str[2*i+1] = str2[len-1-i];
        }
        
        for(int i=0;i<2*len;i++){
            cout<<str[i];
        }
        cout<<endl;
        
        
    }
    
    return 0;
}

发表于 2020-02-17 18:23:56 回复(0)
#include <stdio.h>
#include <string.h>

int main()
{
    char s1[100],s2[100];
    while(scanf("%s%s",s1,s2)!=EOF)
    {
        int l=strlen(s1);
        int p=0,q=l-1;
        for(int i=0;i<l;i++)
        {
            printf("%c%c",s1[p++],s2[q--]);
        }
        printf("\n");
    }
}

发表于 2020-02-11 00:47:09 回复(0)
//这个就是读一个输出一个就OK了,但是得要考虑两个长度不一样的情况
//好像改成或的方式会比较容易,没那么麻烦
#include<bits/stdc++.h>
using namespace std;
int main() {
	char s1[101], s2[101];
	while (cin >> s1 >> s2) {
		int len1 = strlen(s1), len2 = strlen(s2), pos1 = 0, pos2 = len2 - 1;
		for (int i = 0; i<len1 + len2&&pos1 < len1&&pos2 >= 0; i++) {
			if (i % 2 == 0)
				cout << s1[pos1++];
			else
				cout << s2[pos2--];
		}
		if (pos2>=0 && pos1 == len1) {
			for (int i = pos2; i >= 0; i--)
				cout << s2[i];
		}
		else {
			if (pos2 == -1 && pos1 < len1) {
				for (int i = pos1; i < len1; i++)
					cout << s1[i];
			}
		}
	}
}

发表于 2020-01-14 16:11:32 回复(2)
#include<bits/stdc++.h>
int main(){
    char s1[101],s2[101];
    while(gets(s1)){
        gets(s2);
        for(int i=0;i<strlen(s1);i++)//题目说两个字符串等长,所以直接输出
            printf("%c%c",s1[i],s2[strlen(s2)-i-1]);
        printf("\n");
    }
}
编辑于 2019-03-20 16:47:32 回复(0)
#include<stdio.h>
#include<string.h>
int main (){//the shorter,the better.
    int i,j,l;char s1[100],s2[100];
    for(;~scanf("%s%s",s1,s2);){
        for (l=strlen(s1),i=0,j=strlen(s2)-1;i<l||j>=0;i>=l?:printf("%c",s1[i]),j<0?:printf("%c",s2[j]),++i,--j);
        printf("\n");
    }
}

发表于 2018-01-14 18:37:07 回复(0)
#include <cstdio>
#include <cstring>

const int N = 101;

int main()
{
	char s1[N], s2[N], s3[N * 2];
	while (scanf("%s%s", s1, s2) != EOF)
	{
		int len = strlen(s1);
		int j = 0;
		for (int i = 0; i < len; i++)
		{
			s3[j++] = s1[i];
			s3[j++] = s2[len - 1 - i];
		}
		s3[j] = '\0';
		printf("%s\n", s3);
	}
	return 0;
}

发表于 2017-06-05 12:14:08 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
    	Scanner input = new Scanner(System.in);
    	while(input.hasNext()){
    		String s1 = input.next();
    		String s2 = input.next();
    		String s="";
    		int i;
    		for(i=0;i<s1.length()&&i<s2.length();i++)
    		{
    				s=s+s1.charAt(i);
    				s=s+s2.charAt(s2.length()-i-1);
    		}
    		if(s1.length()>s2.length())
    			s=s+s1.substring(i, s1.length());
    		else
    			s=s+s2.substring(i, s2.length());
    		System.out.println(s);
    	}
    	input.close();
    }
}

发表于 2017-04-02 12:41:29 回复(1)
try:
    while 1:
        print ''.join([i for x in zip(raw_input(), raw_input()[::-1]) for i in x])            
except:
    pass

发表于 2016-12-25 00:25:37 回复(0)
//写个简单点的程序
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    string str1,str2;
    while(cin>>str1>>str2)
    {
        string newstring="";
        reverse(str2.begin(),str2.end());
        for(int i=0;i<str1.length();i++)
        {
            newstring=newstring+str1[i]+str2[i];
        }
        cout<<newstring<<endl;
    }
    return 0;
}

发表于 2016-08-22 22:11:36 回复(0)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String str1=sc.next();
String str2=sc.next();
StringBuilder sb=new StringBuilder();
//把第一个字符串放到容器当中
sb.append(str1);
//对第二个字符串逆序输出,添加到容器当中
int k=1;
for(int j=str2.length()-1;j>=0;j--){
sb.insert(k,str2.charAt(j));
k+=2;
}
System.out.println(sb.toString());
}
}
}

发表于 2017-04-13 11:02:10 回复(0)
这道题不难,但是大家形成了惯性思维,什么东西都要准备好了,才敢输出。字符串恰恰不是这样,遍历一个,就可以输出一个!
这道题让我想起了前段时间看到的一段话人生不能像做菜,把所有的料准备好了再下锅。你非要等"齐备"了才开工,可能永远也等不到那一天
#include<stdio.h>
#include<string.h> 
int main(){
char a[101],b[101];
    
while(scanf("%s %s",a,b)!=EOF){
     int len=strlen(a);
    for(int i=0;i<len;i++)
        printf("%c%c",a[i],b[len-1-i]);
    printf("\n");
     }
    return 0;
} 


编辑于 2021-05-22 08:31:23 回复(3)
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s1, s2;
    while (cin >> s1 >> s2) {
        reverse(s2.begin(), s2.end());
        string s;   //合并后的新字符串
        for (int i = 0; i < s1.length() && i < s2.length(); i++) {
            s += s1[i];
            s += s2[i];
        }
        if (s1.length() > s2.length()) {
            s += s1.substr(s2.length());
        }
        if (s2.length() > s1.length()) {
            s += s2.substr(s1.length());
        }
        cout << s << endl;
    }
    return 0;
}

发表于 2024-03-10 17:26:45 回复(0)
两字符串长度相等,最简单的办法是for循环len遍。一个字符一个字符输出
#include <cstdio>
#include <string>
using namespace std;

int main(){
    char s1[101];
    char s2[101];
    while(scanf("%s %s",s1,s2) != EOF){
        string str1 = s1;
        string str2 = s2;
        int len = str1.length();
        for(int i = 0; i < len; ++i){
            printf("%c%c",str1[i],str2[len-1-i]);//一个字符一个字符遍历输出
        }
    }
    return 0;
}


发表于 2023-03-21 21:14:36 回复(0)