急救:c语言中字符串连接出问题,在线等待

HitXU 2003-08-22 11:52:49
字符串(i) = 字符串(i-1) + 字符串(i-2)

看起来很简单,可是我写了几行代码总是不对。

请大侠指点怎么写

...全文
1736 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
MasterGo 2003-08-23
  • 打赏
  • 举报
回复
字符串的末尾有一个结尾符
woaichenyu 2003-08-23
  • 打赏
  • 举报
回复
看来高手还是很多的嘛
hillyee 2003-08-23
  • 打赏
  • 举报
回复
printf(string); ok了
oopig 2003-08-23
  • 打赏
  • 举报
回复
看来十代以上的兔子家族的数目就非常可观了,呵呵
oopig 2003-08-23
  • 打赏
  • 举报
回复
建议你用c++中stl的链表做,轻松很多:

#include <iostream>
#include <list>
#include <assert.h>
using namespace std;

//兔子繁殖一代,即'a'=>'b', 'b'=>'ba'
void breed(list<char> &rabit)
{
list<char>::iterator it = rabit.begin();
while (it != rabit.end())
{
switch (*it)
{
case 'a': //小兔子变成大兔子
*it++ = 'b';
break;
case 'b': //大兔子生出小兔子
rabit.insert(++it, 'a');
break;
default: //这里不知道是什么兔子,略过
assert(false);
it++;
}
}
}
//打印 兔子
void print(const list<char> &rabit)
{
list<char>::const_iterator it = rabit.begin();
for (; it != rabit.end(); it++)
{
cout << *it;
}
cout << endl;
}

int main()
{
list<char> rabit;
rabit.push_back('a'); //预先有一只小兔子
//10代兔子
for (int i = 0; i < 10; i++)
{
print(rabit);
breed(rabit);
}
print(rabit);
return 0;
}
jnqx 2003-08-23
  • 打赏
  • 举报
回复
#include <stdio.h>
#define LEN 1024
#include "stdlib.h"
#include "string.h"

void main()
{
char *f1, *f2, *f3;
int loop;
f1 = (char *)malloc(LEN);
f2 = (char *)malloc(LEN);
f3 = (char *)malloc(LEN);

f1 = "a";
f2 = "b";

for (loop = 0; loop < 3; loop++)
{
strcpy(f3, f2);
strcat(f3, f1);
f1 = f2;
f2 = f3;
printf("%p\n", f1);
printf("%p\n",f2);
printf("%p\n",f3);
}
printf("%s\n", f3);

}
上面的程序我试过了,也不行!
在循环中,我输出了他们的地址,
除了第一次f1的地址不同,其余的都相同!?
3次循环后,地址相同!
所以当printf("%s\n",f3);时,系统不知道是输出f1,还是f2,f3?(我瞎猜的!)
希望大虾们指点一二。
紫郢剑侠 2003-08-22
  • 打赏
  • 举报
回复
用strcat().
Dragon132 2003-08-22
  • 打赏
  • 举报
回复
用函数就行啦
sprintf(字符串(i),"%s%s", 字符串(i-1), 字符串(i-2));
njtu 2003-08-22
  • 打赏
  • 举报
回复
不太清楚你的意思,把你的代码贴出来看看吧。
dddd8888 2003-08-22
  • 打赏
  • 举报
回复
<string.h>
用strcat(char *str, const char*);

必须保证str数组不越界
在c中不能和VB一样字符串相加,除非用C++ stl的string类 或 MFC中的CString类
aflyinghorse 2003-08-22
  • 打赏
  • 举报
回复
要包含头文件
<string.h>
aflyinghorse 2003-08-22
  • 打赏
  • 举报
回复
char *strcat(s,ct) concatenate string ct to end of string s; return s.
其中参数s为char *, ct为const char *
tonybaobao 2003-08-22
  • 打赏
  • 举报
回复
不明白你的意思。
made_in_ 2003-08-22
  • 打赏
  • 举报
回复
如果是string类的,直接相加就可以了:
str = str1 + str2;
如果是字符指针的:
char string[80];
strcpy( string, "Hello world from " );
strcat( string, "strcpy " );
strcat( string, "and " );
strcat( string, "strcat!" );
printf( "String = %s\n", string );
lzy125vc 2003-08-22
  • 打赏
  • 举报
回复
你只要弄清楚两个字符串的基地址,把第二个字符串的基地址连接到第一个字符串的最后一个字符后就行了,很容易实现的.
Dragon132 2003-08-22
  • 打赏
  • 举报
回复
我的程序出了正确结果

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 1024

int main()
{
char f1[LEN],f2[LEN], f3[LEN];
int loop;
//f1 = (char *)malloc(LEN);
//f2 = (char *)malloc(LEN);
//f3 = (char *)malloc(LEN);

strcpy(f1,"a");
strcpy(f2,"b");

for (loop = 0; loop < 10; loop++)
{
strcpy(f3, f2);
strcat(f3, f1);
strcpy(f1,f2);
strcpy(f2,f3);
//f1 = f2;
//f2 = f3;
}
printf("%s\n", f3);
}
yorky 2003-08-22
  • 打赏
  • 举报
回复
你程序的错误就在于
f1 = f2;
f2 = f3;
这两条语句上。在c语言中,字符串赋值不能这么简单的用赋值号,因为f1,f2,f3都是指针,这样做的结果只是这些指针指向的地址发生了变化,而不是字符数组里边的内容发生了变化。
这样,在循环3次后,f1,f2,f3都指向f3字符串的首地址,此时调用strcat(f3, f1);
便会产生一个访问错误退出。
正确的做法应该如aflyinghorse的做法一样。
刚开始用
strcpy(f1, f2);
strcpy(f2, f3);
语句替换

f1 = f2;
f2 = f3;
后,在执行strcpy(f1, f2);的时候也会产生访问错误。不过这个错误是为什么呢?因为
f1="a"的缘故吗?
aflyinghorse 2003-08-22
  • 打赏
  • 举报
回复
#define LEN 1024
int main()
{
char *f1, *f2, *f3, *temp;
int loop;
f1 = (char *)malloc(LEN);
f2 = (char *)malloc(LEN);
f3 = (char *)malloc(LEN);
temp = (char *)malloc(LEN);

strcpy(f1, "a");
strcpy(f2, "b");
strcpy(temp, f2);
strcpy(f3, "a b ");

for (loop = 0; loop < 5; loop++)
{

strcat(f2, f1);

strcpy(f1, temp);
strcpy(temp, f2);

strcat(f3, f2);
strcat(f3, " ");

}
printf("%s\n", f3);

free(f1);
free(f2);
free(f3);
free(temp);
getchar();
}
hellobcb 2003-08-22
  • 打赏
  • 举报
回复
是大括号。呵呵!
hellobcb 2003-08-22
  • 打赏
  • 举报
回复
哦!少了一个分号。
加载更多回复(17)

69,364

社区成员

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

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