问题 C: 字符串合并(指针与函数)

问题 C: 字符串合并(指针与函数)

时间限制: 1 Sec  内存限制: 128 MB
提交: 219  解决: 151
[提交][状态][讨论版]

题目描述

定义一个函数char *strAdd(char *s1, char *s2),其返回的是s1和s2两个字符串合并后的值。(要求不能改变s1和s2)

输入

测试数据的组数 t

第一组字符串1

第一组字符串2

第二组字符串1

第二组字符串2

.......

输出

第一组字符串1 第一组字符串2 第一组字符串1和字符串2的合并

第二组字符串1 第二组字符串2 第二组字符串1和字符串2的合并

.........

样例输入

2

hello world

string concatation

样例输出

hello world helloworld

string concatation stringconcatation

 

#include <iostream>
#include <cstring>
using namespace std;
char *start;
char *p=new char[100];
char *strAdd(char *s1,char *s2){
    start=p;
    char *p1=s1;
    char *p2=s2;
    //其实也可以用for循环做,使用*(p+i),这里试试另外的方法
    while (*s1!='\0') {
        *p++=*s1++;
    }
    while (*s2!='\0') {
        *p++=*s2++;
    }
    while (*p1!='\0'){
        cout<<*p1;
        p1++;
    }
    cout<<" ";
    while (*p2!='\0'){
        cout<<*p2;
        p2++;
    }
    cout<<" ";
    return start;//不能使用局部空间
}
int main() {
    int t;
    cin>>t;
    while(t--){
        char str1[100];
        char str2[100];
        cin>>str1>>str2;
        char *add=strAdd(str1,str2);
        while (*add!='\0'){
            cout<<*add;
            add++;
        }
        cout<<endl;
    }
    return 0;
}

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值