首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

二维数组如何转换成一维数组?在每一维后加'\n'。

2012-03-02 
二维数组怎么转换成一维数组?在每一维后加\n。在线等!~急~~~~比如p[][]aaaabbbccccc转换成tempaaaa\nbb

二维数组怎么转换成一维数组?在每一维后加'\n'。在线等!~急~~~~
比如
p[][]=aaaa
            bbb
            ccccc
转换成temp   =   aaaa\nbbb\nccccc
我是这样做的

temp   =   p[0];
for(i=0;i <row;i++)
{
len=strlen(temp);temp[len]   =   '\n ';
if(i==row-1)temp[len]   =   '\0 ';

}
可是   最终结果temp   '\n '的ASCLL码却是   10   也就是变成 '\r '
请问   这是怎么回事?
还是我写错了?   如果是我写错了   请告诉我错在那里?谢谢前辈!~

[解决办法]
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
char p[][10]={ "aaaa ", "bbb ", "cc ", NULL};
char *tmp=(char *)malloc(40*sizeof(char));
int i=0, index=0;

while(strcmp(p[i], " "))
{
int j=0;
while(p[i][j] != '\0 ') {tmp[index++]=p[i][j]; j++;}
tmp[index++]= '\n ';
i++;
}
tmp[--index]= '\0 '; //这个是为了覆盖最后的一个多余的 \n
puts(tmp);

system( "PAUSE ");
return 0;
}
[解决办法]
#include <stdio.h>

int main()
{
char p[][4]={ "abc ", "bcd ", "efg "};
char *temp = p[0];
int low=3,i,n;
for(i=0;i <low;i++)
{
if(i==low-1)
{
n = strlen(temp);
}
temp[strlen(temp)] = '\n ';
}
temp[n] = 0;
puts(temp);

return 0;
}

热点排行
Bad Request.