![]() |
#2
lknight2012-05-21 21:49
|
用字符数组,不用strcat函数,将两个字符串连接起来
代码如下:

#include <iostream>
using namespace std;
int main()
{
int i,j;
char c1[]={"Huizhou "},c2[]={"xueyuan"};
const int a=strlen(c1),b=strlen(c2);
const int k=a+b;
char c3[k];
for(i=0;i<=a-1;i++)
c3[i]=c1[i];
for(i=a;i<=a+b-1;i++)
for(j=1;j>j+1;j++)
c3[i]=c2[j];
return 0;
}
using namespace std;
int main()
{
int i,j;
char c1[]={"Huizhou "},c2[]={"xueyuan"};
const int a=strlen(c1),b=strlen(c2);
const int k=a+b;
char c3[k];
for(i=0;i<=a-1;i++)
c3[i]=c1[i];
for(i=a;i<=a+b-1;i++)
for(j=1;j>j+1;j++)
c3[i]=c2[j];
return 0;
}
编译时出错,求教~谢谢了~
--------------------Configuration: sy5_13 - Win32 Debug--------------------
Compiling...
sy5_13.cpp
C:\Documents and Settings\ZJJ\桌面\sy5_13\sy5_13.cpp(10) : error C2057: expected constant expression
C:\Documents and Settings\ZJJ\桌面\sy5_13\sy5_13.cpp(10) : error C2466: cannot allocate an array of constant size 0
C:\Documents and Settings\ZJJ\桌面\sy5_13\sy5_13.cpp(10) : error C2133: 'c3' : unknown size
执行 cl.exe 时出错.
sy5_13.obj - 1 error(s), 0 warning(s)