社区
C语言
帖子详情
急救:c语言中字符串连接出问题,在线等待
HitXU
2003-08-22 11:52:49
字符串(i) = 字符串(i-1) + 字符串(i-2)
看起来很简单,可是我写了几行代码总是不对。
请大侠指点怎么写
...全文
1788
37
打赏
收藏
急救:c语言中字符串连接出问题,在线等待
字符串(i) = 字符串(i-1) + 字符串(i-2) 看起来很简单,可是我写了几行代码总是不对。 请大侠指点怎么写
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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)
这是我们的试题
- **C++实现**:使用
字符串
比较函数`strcmp`查找学生姓名,或直接比较整型变量来查找学号。 - **示例代码**: ```cpp int srchByname(const Student *p, int n, const string &name) { for (int i = 0; i ; ++i) ...
MySQL5 权威指南第3版
中
文版_part1
15.5
字符串
、日期、时间、BLOB和NULL 15.6 向关联数据表插入新数据记录 15.7 处理来自HTML表单的输入数据 15.8 分页显示查询结果 15.9 处理层次化数据 15.10 速度优化 15.11 Unicode 15.12 二进制...
编程之法学习笔记——1.1
字符串
的旋转
题目描述:给定一个
字符串
,要求将
字符串
前面的若干个字符移到
字符串
的尾部。例如,将
字符串
"abcdef"的前3个字符'a','b'和'c'移到
字符串
的尾部,...原
字符串
中
的x~n位的字符移动到新
字符串
的0~(n-x)位。...
C语言
结构体录入字符到文件,在
C语言
中
,如何将结构体输入文件?
急救
啊!
→小盆友君←回答数:192|被采纳数:822017-01-23 23:49:17#include#includestruct Person{char name[100];int age;int sex;char add[100];};int main(int argc, char* argv[]){FILE *FIN;char STRING_COMMAND[30];...
c语言
中
常用的输入输
出
函数有哪些,
C语言
中
常用的输入和输
出
函数
C语言
中
常用的输入和输
出
函数2020年03月11日|萬仟网IT编程 |我要评论1. scanf()函数 (Scan Format) + 函数原型: + 函数作用:从标准输入流 stdin (标准输入设备,一般指向键盘)读取输入,并根据提供的 format 来...
C语言
70,038
社区成员
243,247
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章