#include<iostream>
using namespace std;
char* stringcatQ(char* b1,char* b2)
{
int length1,length2;
length1=strlen(b1);
length2=strlen(b2);
char* c=new char[length1+length2+1];
for(int i=0;i<length1;i++)
{
c[i]=b1[i];
}
int n=0;
for(int k=length1;k<length1+length2;k++)
{
c[k]=b2[n];
n++;
}
c[length1+length2]='\0';
return c;
}
int main()
{
char a1[80]="jape";
char a2[20]="dogcat";
char* newchar=stringcatQ(a1,a2);
cout<<newchar<<endl;
delete newchar;
return 0;
}//有错误 ,只考虑到返回值了,没考虑形参和实参。。
----------------------------------------------------------------------------
对上面程序的改进,不过很难看懂啊 ,我自己都迷糊了。
#include<iostream>
using namespace std;
char* stringcatQ(char* b1,char* b2)
{
int length1,length2;
length1=strlen(b1);
length2=strlen(b2);
char* c=new char[length1+length2+1];
for(int i=0;i<length1;i++)
{
b1[i]=c[i]=b1[i];
}
int n=0;
for(int k=length1;k<length1+length2;k++)
{
b1[k]=c[k]=b2[n];
n++;
}
c[length1+length2]='\0';
b1[length1+length2]='\0';
return c;
}
int main()
{
char a1[80]="jape";
cout<<a1<<endl;
char a2[20]="dogcat";
char* newchar=stringcatQ(a1,a2);
cout<<newchar<<endl;
cout<<a1;
delete newchar;
return 0;
}
----------------------------------------------------------------------------------------------------------------------
继续改进
#include<iostream>
using namespace std;
char* stringcatQ(char* b1,char* b2)
{
int length1,length2;
length1=strlen(b1);
length2=strlen(b2);
int n=0;
for(int k=length1;k<length1+length2;k++)
{
b1[k]=b2[n];
n++;
}
b1[length1+length2]='\0';
return b1;
}
int main()
{
char a1[80]="jape";
cout<<a1<<endl;
char a2[20]="dogcat";
char* newchar=stringcatQ(a1,a2);
cout<<newchar<<endl;
cout<<a1<<endl;
return 0;
}
-----------------------------------------------------------------------------------
原型
#include<iostream>
using namespace std;
char* cat(char* dest,const char* sour)
{
char *returnchar=dest;
while(*dest)
{
dest++;
}
while(*dest=*sour)//还可以写成*dest++=*sout++,循环体内为空
{
dest++;
sour++;
}
cout<<&dest<<" "<<endl;
return returnchar;
}
int main()
{
char a[100]="how do ";
char b[]="you do";
char* f=cat(a,b);
cout<<f<<endl;
return 0;
}