注册 登录
编程论坛 VC.NET论坛

字符数组问题

jerry820726 发布于 2006-07-06 15:10, 1703 次点击

现有3个字符数组: char a[9999],char b[9999],char c[9999],如何把这三个字符数组 合并成一个 char[99999];和 string aa; 高手提点下

9 回复
#2
xupeng2006-07-06 16:56
char d[99999];
int i=0;
for(int j=0;j<9999;j++)
{
if(a[j]!=null)
{
d[i]=a[j];
i++;
}
else
break;
}
for(int j=0;j<9999;j++)
{
if(b[j]!=null)
{
d[i]=b[j];
i++;
}
else
break;
}
for(int j=0;j<9999;j++)
{
if(c[j]!=null)
{
d[i]=c[j];
i++;
}
else
break;
}
#3
jerry8207262006-07-07 08:49
2楼的  谢谢
#4
xupeng2006-07-07 13:05
不用客气
#5
十一月天2006-07-07 13:26
xupeng你的算法有点麻烦啊
#6
xupeng2006-07-07 13:38
以下是引用璀璨星河在2006-7-7 13:26:10的发言:
xupeng你的算法有点麻烦啊

我也是菜鸟,麻烦你弄个简单的算法!

#7
jerry8207262006-07-08 14:57

是啊 5楼的有简单的 弄个我门看看啊

#8
xupeng2006-07-09 19:08

不知道!
请高手指点!

#9
十一月天2006-07-11 11:30
我觉得用
for(int i=0;i<lenth(a)+lenth(b)+lenth(c);i++)
就可以了。

你写的:
for(int j=0;j<9999;j++)
{
if(a[j]!=null)
{
d[i]=a[j];
i++;
}
else
break;
}
其中if(a[j]!=null),是什么?null代表空,只要a[]这个数组赋值了,就没有空的,这个是多余的,楼主也没说清楚,到底赋值过什么。再有,假如a[100]这个值是空,而a[101]这个值是有值的,你这么写,a[101]的值就丢了,当然这只是假设,因为我可以单独用:a[100]=null; 这个办法赋值。
#10
myajax952006-07-11 13:58

一行的程序呀。变char[99999]:
#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;

void main()
{
char a[9999], b[9999], c[9999],d[99999];
sprintf(d, "%s%s%s", a,b,c);

stringstream ss;
ss << a << b << c;
string aa = ss.str();
}

1