HDU 2567 寻梦(字符串,插入)

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
int main()
{
   int t;
   char a[55],b[55],r[120];//b插入a中央
   scanf("%d",&t);
   while(t--)
   {
       scanf("%s%s",a,b);//可以用gets
       int lena=strlen(a);
       int lenb=strlen(b);
       int i=0,j=0;
       while(a[i]!='\0'&&i<lena/2)//确保要插入的位置<=要插入的字符串的长度  
       {
           r[i]=a[i];
           i++;
       }
       while(b[j]!='\0')
       {
           r[i+j]=b[j];
           j++;
       }
       while(a[i]!='\0')//将剩余的字符串复制
       {
           r[i+j]=a[i];
           i++;
       }
       r[i+j]='\0';
       puts(r);
   }

}

  

#include <stdio.h>
#include <string.h>
int main() {
    int n;
    scanf("%d\n",&n);
    while (n--) {
        char a[100],b[100];
        int i,j;
        gets(a);
        gets(b);
        int lena=(int)strlen(a),lenb=(int)strlen(b);
        for (i=0; i<(lena/2); i++) {
            printf("%c",a[i]);
        }
        for (j=0; j<lenb; j++) {
            printf("%c",b[j]);
        }
        for (i=(lena/2); i<lena; i++) {
            printf("%c",a[i]);
        }
        printf("\n");
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/Roni-i/p/7206290.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值