首页 > 其他分享 >传智杯 第六届—A

传智杯 第六届—A

时间:2024-10-08 12:53:50浏览次数:3  
标签:输出 传智杯 s2 s1 第六届 拼接 数组 字符串

题目:

键盘输入两个字符串,将这两个字符串进行拼接后输出。

输入描述:

键盘输入两个字符串

输出描述:

输出两个字符串拼接后的结果

输入

hello
nihao

输出

hellonihao

解题思路:

        本题是需要将两个字符串s1和s2拼接在一起并进行输出。由于字符串可以由数组来进行表示,那么我们可以将字符串s1和s2中的内容存入到一个数组中,最后将这个数组进行输出就可以得到两个字符串拼接后的结果。

代码:

#include <iostream>
#include <string>
#define MAX 1000
using namespace std;

int main() {
        
    string s1, s2;
    getline(cin, s1);
    getline(cin, s2);
 
    // write your code here......
    char array[MAX]; 
    int i=0;
    
    //将s1中的内容存入到数组中
    for(;i<s1.length();i++)
    {
        array[i] = s1[i];
    }

    //将s2中的内容存入到数组中
    for(int j=0;j<s2.length();j++)
    {
        array[i++] = s2[j];
    }

    将数组进行输出得到两个字符串拼接过后的结果
    for(int j=0;j<s1.length()+s2.length();j++)
    {
        cout<<array[j];        
    }
    cout<<endl;

    return 0;
}

标签:输出,传智杯,s2,s1,第六届,拼接,数组,字符串
From: https://blog.csdn.net/weixin_66512566/article/details/142755699

相关文章