jerry820726 发表于 2006-7-6 15:10

字符数组问题

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

xupeng 发表于 2006-7-6 16:56

char d[99999];<BR>int i=0;<BR>for(int j=0;j&lt;9999;j++)<BR>{<BR>    if(a[j]!=null)<BR>    {<BR>        d[i]=a[j];<BR>        i++;<BR>    }<BR>    else<BR>    break;<BR>}<BR>for(int j=0;j&lt;9999;j++)<BR>{<BR>    if(b[j]!=null)<BR>    {<BR>        d[i]=b[j];<BR>        i++;<BR>    }<BR>    else<BR>    break;<BR>}<BR>for(int j=0;j&lt;9999;j++)<BR>{<BR>    if(c[j]!=null)<BR>    {<BR>        d[i]=c[j];<BR>        i++;<BR>    }<BR>    else<BR>    break;<BR>}

jerry820726 发表于 2006-7-7 08:49

2楼的  谢谢[em01]

xupeng 发表于 2006-7-7 13:05

不用客气[em01]

十一月天 发表于 2006-7-7 13:26

<STRONG><FONT color=#000066>xupeng[em03]你的算法有点麻烦啊</FONT></STRONG>

xupeng 发表于 2006-7-7 13:38

<DIV class=quote><B>以下是引用<U>璀璨星河</U>在2006-7-7 13:26:10的发言:</B><BR><STRONG><FONT color=#000066>xupeng[em03]你的算法有点麻烦啊</FONT></STRONG></DIV>
<P>我也是菜鸟,麻烦你弄个简单的算法![em04]</P>

jerry820726 发表于 2006-7-8 14:57

<P>是啊 5楼的有简单的 弄个我门看看啊</P>

xupeng 发表于 2006-7-9 19:08

<P>不知道![em04]<BR>请高手指点![em04]</P>

十一月天 发表于 2006-7-11 11:30

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

myajax95 发表于 2006-7-11 13:58

<P>一行的程序呀。变char[99999]:<BR>#include &lt;stdio.h&gt;<BR>#include &lt;string&gt;<BR>#include &lt;sstream&gt;<BR>using namespace std;</P>
<P>void main()<BR>{<BR>char  a[9999],  b[9999],  c[9999],d[99999];<BR>sprintf(d, "%s%s%s", a,b,c);</P>
<P>stringstream ss;<BR>ss &lt;&lt; a &lt;&lt; b &lt;&lt; c;<BR>string aa = ss.str();<BR>}<BR></P>

页: [1]

编程论坛