7、在字符串前加5个符号"note:"

老师的方法:

#include<iostream>
using namespace std;
int main()
{
	char str1[40]={"I am a boy."};
	char str2[20]={"note:"};
	int i=0,len2=0;
	//下面将把str2插入到str1的开头部分,并且保存在str1中
	//先求出str2的“长度”
	while(str2[len2]!='\0')
	{  
		len2++;
	}	
	//str2中有len2个字符
	//找到str1中结尾的位置
	i=0;    
	while(str1[i]!='\0')
	{
		i++;
	}
	//由后往前,整体往后“搬迁”len2个位置
	while(i>=0)
	{
		str1[i+len2]=str1[i];
		i--;
	}
	//空出的前len2位置复制为str2
	i=len2-1;
	while(i>=0)
	{
		str1[i]=str2[i];
		i--;
	}
	cout<<"处理后的字符串是: "<<str1<<endl;
	return 0;
}

自己的方法:(str2的长度自己直接写了。。)

#include <iostream>
using namespace std;
#include<iostream>
using namespace std;
int main()
{
    char str1[50]="I am a happy boy\'s daddy.",str2[50]="note:";
    int i=0;
    while(str1[i]!='\0')
    {
        i++;
    }
     cout<<i<<endl;
     cout<<str1[i]<<endl;
    for(int j=i-1;j>=0;j--)
      str1[j+5]=str1[j];
    for(int j=0;j<5;j++)
      str1[j]=str2[j];
    cout<<str1<<endl;
    //cout<<"整理后的字符串"<<str2<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值