将a序列头部和b序列尾部交替插入到c序列中

代码如下:

#include<iostream>
using namespace std;
void hebing(char a[], char b[],char c[], int length1, int length2)//默认a短
{
	int cc = 0;
	for (int i = 0; i <= length1 - 1; i++)
	{
		c[cc] = a[i];
		cc++;
		c[cc] = b[length2 - 1 - i];
		cc++;
	}
	for (int i = length1; i <= length2 - 1; i++)
	{
		c[cc] = b[length2 - i - 1];
	}
}
int main()
{
	char a[100];
	char b[100];
	cin >> a;
	cin >> b;
	int length1 = 0;
	for (int i = 0; a[i]!='\0';)
	{
		i++;
		length1++;
	}
	int length2 = 0;
	for (int i = 0; b[i] != '\0';)
	{
		i++;
		length2++;
	}
	char c[200];
	hebing(a, b, c, length1, length2);
	for (int i = 0; i <= length1 + length2 - 1; i++)
	{
		cout << c[i];
	}
	return 0;
}

错误1:

cin>>a;cin>>b;

放在了计算长度的代码段后面。
改正1:
应放在初始化字符数组之后,计算长度的代码段之前。
− − − − − − \color{#4285f4}{-}\color{#ea4335}{-}\color{#fbbc05}{-}\color{#4285f4}{-}\color{#34a853}{-}\color{#ea4335}{-} − − − − − − \color{#4285f4}{-}\color{#ea4335}{-}\color{#fbbc05}{-}\color{#4285f4}{-}\color{#34a853}{-}\color{#ea4335}{-} − − − − − − \color{#4285f4}{-}\color{#ea4335}{-}\color{#fbbc05}{-}\color{#4285f4}{-}\color{#34a853}{-}\color{#ea4335}{-} − − − − − − \color{#4285f4}{-}\color{#ea4335}{-}\color{#fbbc05}{-}\color{#4285f4}{-}\color{#34a853}{-}\color{#ea4335}{-} − − − − − − \color{#4285f4}{-}\color{#ea4335}{-}\color{#fbbc05}{-}\color{#4285f4}{-}\color{#34a853}{-}\color{#ea4335}{-} − − − − − − \color{#4285f4}{-}\color{#ea4335}{-}\color{#fbbc05}{-}\color{#4285f4}{-}\color{#34a853}{-}\color{#ea4335}{-}
错误2:
计算通过cin>>输入的字符数组长度的代码段写成了这样:

for(int i=0;a[i];)
    {
        i++;
        length1++;
    }

改正2:
实际上cin>>输入的字符数组是由’\0’结尾的,而单个对字符数组赋值则要手动在末尾加上’\0’。所以代码应该是这样的:

for(int i=0;a[i]!='\0';)
    {
        i++;
        length1++;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值