【我要循环处理输入的字符串,每次长度不定,但处理完毕后要连接在一起,如何将结果存放在一个指针字符呢】
我要循环特殊处理输入的字符串,每次长度不定,但处理完毕后要连接在一起,如何将结果存放在一个指针字符呢?
该如何对指针字符进行操作或者使用函数呢
[解决办法]
gets();接收
strcat()连接
用malloc()动态申请空间
[解决办法]
//.........
string sInfo;
char *cInfo = new char(1024);
char *whileChar[...];
while (i....)
{
string temp(whileChar[i]);
sInfo += temp;
}
memcpy(cInfo, sInfo.c_str(), ...);
[解决办法]
#include "stdio.h "
#include "string.h "
#include "stdlib.h "
void main()
{
char *p[100],*q;;
char rec[100];
int i,j,count;
count=0;
i=0;
while(1)
{
printf( "input Enter to quit ,please input :\n ");
fflush(stdin);
gets(rec);
if(rec[0]== '\0 ')
break;
p[i]=(char *)malloc((strlen(rec)+1)*sizeof(char));
strcpy(p[i],rec);
i++;
if(i> 99)
break;
}
for(j=0;j <i;j++)
count+=strlen(p[j]);
q=(char *)malloc((count+1)*sizeof(char));
*q= '\0 ';
for(j=0;j <i;j++)
{
strcat(q,p[j]);
free(p[j]);
}
printf( "%s\n ",q);
free(q);
}