【我要循环处理输入的字符串,每次长度不定,但处理完毕后要连接在一起,如何将结果存放在一个指针字符呢】

flycsdn 2007-06-13 10:37:11
我要循环特殊处理输入的字符串,每次长度不定,但处理完毕后要连接在一起,如何将结果存放在一个指针字符呢?
该如何对指针字符进行操作或者使用函数呢
...全文
381 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyaoyou 2007-06-14
  • 打赏
  • 举报
回复
把str1和str2当作是你需要处理的字符串就好了,str[i]就代表着其中的每个字符。
liuyaoyou 2007-06-14
  • 打赏
  • 举报
回复
单纯进行字符串连接:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void main(void)
{
char *str1="abc";
char *str2="def";
int length1=strlen(str1);
int length2=strlen(str2);
int length=length1+length2;
char *str=(char *)malloc(length+1);
strcpy(str,str1);
strcat(str,str2);
printf("%s\n",str);
for(int i=0;i<length;i++)
{
printf("%c ",str[i]);
}
printf("\n");
free(str);
}
huashizhixin 2007-06-14
  • 打赏
  • 举报
回复
vc 6.0
#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);
}
czlyc006 2007-06-14
  • 打赏
  • 举报
回复
感觉上如果可以用string的方法的话会简单很多...
zucczgc 2007-06-14
  • 打赏
  • 举报
回复
liuyaoyou 这个代码是肯能出现问题的,很有机会会出现内存重叠拷贝...多运行几次的话 结果只有一个coredump 嘿嘿. 用realloc吧 不要用拷贝 直接用strcat
#include <stdio.h>
#include <string.h>

#define __BUFFER_LINE__ (1024)
char* p_input_record = NULL;
int main(int argc, char* argv[])
{
char buf[__BUFFER_LINE__];
memset(buf, 0, sizeof(buf));
do{
......... /*这里做输入的代码*/
/*这里调用一个字符串连接函数*/
}while(1);
return 0;
}
static void strconcat(char* tempstr)
{
if(!p_input_record)
{
p_input_record = (char*)malloc(strlen(tempstr)+1);
if(!p_input_record)
exit(1);
strcpy(p_input_record, tempstr);
}
else
{
p_input_record = realloc(p_input_record, strlen(p_input_record)+ strlen(tempstr)+1);
strcat(p_input_record, tempstr);
}
}
hamlet0168 2007-06-13
  • 打赏
  • 举报
回复
没给你写注释,慢慢看吧。希望对你有帮助。如果用STL,代码就更简单了。
hamlet0168 2007-06-13
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
void main()
{
int istep=1;
int max_size = 1024;
char * pfinal=new char[istep * max_size];
memset(pfinal,0,istep * max_size);
char * str=new char[istep * max_size];
int cnt=0;
while(cin.getline(str,max_size,'\n'))
{
if(!strcmp(str,"exit"))
break;
cnt += cin.gcount();
if( cnt > istep*max_size -1 )
{
istep++ ;
char * tmp = pfinal;
pfinal=new char[istep * max_size];
strcpy(pfinal,tmp);
delete [] tmp;
tmp=0;
}
strcpy(pfinal+strlen(pfinal),str);
}
cout<<pfinal<<endl;
if(str)
{
delete [] str;
str=0;
}
if(pfinal)
{
delete[] pfinal;
pfinal=0;
}
system("pause");
}
bargio_susie 2007-06-13
  • 打赏
  • 举报
回复
那循环的次数,申请的输入每个字符串长度,最后的总长度自己看着办。。。。
bargio_susie 2007-06-13
  • 打赏
  • 举报
回复
是不是想这样的??

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char *desStr;
int i;
char *str[10];


for (i=0; i<10; i++)
str[i] = (char *)malloc(20);
desStr = (char *)malloc(100);
strcpy(desStr, "\0");
for (i=0; i<10; i++)
{
scanf("%s", str[i]);
strcat(desStr, str[i]);
}
printf("%s\n", desStr);

for (i=0; i<10; i++)
free(str[i]);
free(desStr);

return 0;
}
flycsdn 2007-06-13
  • 打赏
  • 举报
回复
huashizhixin可否给个演示代码?
flycsdn 2007-06-13
  • 打赏
  • 举报
回复
我要循环特殊处理输入的字符串,每次输入的长度不定,
处理完之后依然是字符串,我要将这些处理完毕后的字符串要连接在一起,如何将结果存放在一个指针字符呢?每次处理结果都存放在指针里面,如何连接所有的处理结果?
该如何对指针字符进行操作或者使用函数呢
huashizhixin 2007-06-13
  • 打赏
  • 举报
回复
gets();接收
strcat()连接
用malloc()动态申请空间
alxen 2007-06-13
  • 打赏
  • 举报
回复
lz说具体点~~~
growleaf 2007-06-13
  • 打赏
  • 举报
回复
什么意思啊???

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧