不用C库函数。字符串连接问题。

yang2507366 2006-08-18 08:06:10
输入两个字符串,将它们连接起来,不用库函数。请问怎么做啊?
...全文
536 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
JavaerRobertLi 2006-08-22
  • 打赏
  • 举报
回复
char a[]="af";
char b[]="ds";
char *q=a,*p=b;
char f[50];

int x=0;
while(*q)
f[x++]=*(q++);
while(*p)
f[x++]=*(p++);
f[x]='\0';
cout<<f;
muroachanf 2006-08-20
  • 打赏
  • 举报
回复
数组和指针用了那么久,感觉区别很小,用法基本一样。
Norris_Zhang 2006-08-20
  • 打赏
  • 举报
回复
多谢。我多来发表我的代码,大家多指点。
rollor_phoe 2006-08-20
  • 打赏
  • 举报
回复
上面搞错了,应该如下:
char a[]="abcd";
char b[]="efgh";
char c[10];
for (int i=0; i< sizeof(a)/sizeof(char)-1; ++i)
{
c[i]=a[i];
}
for (int j=(sizeof(a)/sizeof(char)-1),k=0;
j<(sizeof(a)/sizeof(char)+sizeof(b)/sizeof(char));)
{
c[j++]=b[k++];
}
rollor_phoe 2006-08-20
  • 打赏
  • 举报
回复
char a[]="abcd";
char b[]="efgh";
char c[10];
for (int i=0; i< sizeof(a)/sizeof(char); ++i)
{
c[i]=a[i];
}
for (int j=sizeof(c)/sizeof(char),int k=0;
j<(size(a)/sizeof(char)+sizeof(b)/sizeof(char);)
{
c[j++]=b[i++];
}
binglex 2006-08-20
  • 打赏
  • 举报
回复
arkwrightzhn(阿克赖特qq673188423,arkwright@21cn.com:

1. 其实你写的2个for循环我写的那个while循环功能是一样的;

2. 指针和数组都是需要的,只是数组作为参数传递后就变成指针了,而不是数组,所以作为一个独立函数时参数就是指针了,你可以试试把你的2个for循环作成strcat那种函数就明白了。

3. 说不用库函数只是说在进行串连接时不要用,而不是一直不用,而且lz要的只是一个函数而不是整个程序。
Norris_Zhang 2006-08-19
  • 打赏
  • 举报
回复
我只会数组,好象这里的高手都用指针,我还没有学到指针,学了指针就不用数组了吗?那数组就是一个中途学习的工具吗?是不是开发实践中都不用数组?

#include < iostream.h >
#include < stdlib.h >
int main()
{
char a[80]; b[80];
int i, j;
cout << "请输入两个字符串:" << endl;
cin.getline(a, 80);
cin.getline(b, 80);
for(i=0; a[i]!='\0'; i++)
{}
for(j=i, i=0; b[i]!='\0'; j++, i++)
a[j]=b[i];
a[j]='\0';
cout << "连接后:" << a << endl;
system("pause");
return 0;
}

cin.getline()算库函数吗?要不让用我就不会了。
飞哥 2006-08-19
  • 打赏
  • 举报
回复
看来真是新手专区

第一个串游标移到末尾,将第二个串拷贝过去就ok了
不过要申请足够的空间
iambtk 2006-08-19
  • 打赏
  • 举报
回复
用两个链表存储两个字符串。
先遍历第一个链表,当其P->NEXT==NULL的时候,将第二个链表的插入就行。
binglex 2006-08-19
  • 打赏
  • 举报
回复
lj860603(键键)的比较简单明了,就是技巧性太强了,我还是喜欢这样:
void My_StrCat(char *Source, const char *String)
{
while (*Source)Source++;
while (*String)*Source++ = *String++;
*Source=0;

}

另外这种东西用c和c++没什么区别;
plamlover 2006-08-19
  • 打赏
  • 举报
回复
正如 雁南飞 所说的一样.
char* StrCat(char* Pa,char* Pb)
{
char* Px,*Py;
int i;
Px=Pa;
Py=Pb;
while(*Px!='\0')
Px++;
for(i=0;i<strlen(Pb);i++,Px++,Py++)
//strlen同样可以自己写出来,它比较简单所以就未写了
{
*Px=*Py;
}
return Pa;
}
yang2507366 2006-08-19
  • 打赏
  • 举报
回复
不是用C++。用C语言 谢谢
OpenHero 2006-08-18
  • 打赏
  • 举报
回复
char * p1 = "test1";
char * p2 = "test2";
int n = 0;
int m = 0;
char * pp1 = p1;
char * pp2 = p2;
while(*pp1++)
n++;
while (*pp2++)
m++;

char* p_str = new char[m+n];
cout << p_str << endl;
int i = 0;
while (n--)
{
p_str[i++] = p1[i];
}
cout << p_str <<endl;
int j = 0;
while (m--)
{
p_str[i++] = p2[j++];
}
cout << p_str <<endl;
cout << n+m <<endl;
delete [] p_str;
lj860603 2006-08-18
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>

void My_StrCat(char *Source, const char *String)
{
while (*Source++)
;
for (--Source; *Source++ = *String++; )
;
}

int main(void)
{
char source[30] = "abcdefg";
char string[30] = "hijklmn";

My_StrCat(source, string);

puts(source);

system("PAUSE");
return 0;
}

33,312

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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