Codeup 1785

Codeup 1785 字符串连接

题目描述

不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。

输入

每一行包括两个字符串,长度不超过100。

输出

可能有多组测试数据,对于每组数据,
不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。
输出连接后的字符串。

样例输入

abc def

样例输出

abcdef

初始代码

#include<cstdio>
#include<cstring>
int max=100;
int main(){
	char str1[max],str2[max];
	while(gets(str1)&&gets(str2)){/输入时是同行输入不是异行输入且并未验证是否为EOF
		char str[200];
		int len1=strlen(str1);
		int i;
		for( i=0;i<len1;i++)
		str[i]=str1[i];
		int len2=strlen(str2);
		for(int j=0;j<=len2;j++ ){
			
			str[i]=str2[j];
			i++;
		}
		printf("%s\n",str);
		
	}
	
}

若改为scanf("%s%s",&str1,&str2)会出现以下情况
在这里插入图片描述

最终代码

#include<cstdio>
#include<cstring>
int max=100;
int main(){
	char str1[max],str2[max];
	while(scanf("%s%s",&str1,&str2)!=EOF){
		char str[200];
		int len1=strlen(str1);
		int i;
		for( i=0;i<len1;i++)
		str[i]=str1[i];
		int len2=strlen(str2);
		for(int j=0;j<=len2;j++ ){
			
			str[i]=str2[j];
			i++;
		}
		printf("%s\n",str);
		
	}
	
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值