老师的方法:
#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;
}