字符串输出问题2

hahaysw 2008-03-03 10:53:46
#include<iostream.h>
#include<string.h>
void piglatin(char *a)
{
char *b=a;
char c=*b;
char *e="ay";
char d[20]="";
char *f=d;
for(int z=0;*b!='\0';b++,z++)
{
if(*(b+1)!='\0')
d[z]=*(b+1);
else
d[z]=c;
}
strcat(d,e);
cout<<d<<'\n';
}
int main()
{
char a[100]="jump the computer";
char *b=strtok(a," ");
piglatin(b); //输出umpjay
cout<<b; //输出jump到这都没问题
return 0;
}
//但是在return语句后面加入以下代码程序就运行不了了,为什么?
//由于在上面输出b时证明b没被修改,所以我认为后面的运用函数strtok的格式应该没问题。
//while(b!=NULL)
//{
// b=strtok(NULL," ");
// piglatin(b);但是就算没有了这行而在下一行输出b也运行不了程序,不知道错在哪?
//}以下是书上的strtok函数的举例应用。
#include<iostream.h>
#include<string.h>
int main()
{
char string[]="this is a sentence with 7 tokens";
char *tokenptr;
cout<<"the string to be tokenized is:\n"<<string
<<"\n\nthe tokens are:\n";
tokenptr=strtok(string," ");
while(tokenptr!=NULL)
{
cout<<string<<'\n';
cout<<tokenptr<<'\n';
tokenptr=strtok(NULL," ");
}
return 0;
}
...全文
62 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hahaysw 2008-03-03
  • 打赏
  • 举报
回复
太感谢你了~非常感谢~是你的提示给了我启发。
原来我错在位置问题。真是想不到
b=strtok(NULL," ");
piglatin(b);
这2行的位置居然不能调换。
其实我就是想找出1个能修改strtok函数标记后的元素的方法。
开始我试,1个个的试,从字符数组到字符指针,再到字符指针数组,
居然都不行,我还以为方法错了。想不到是位置~
再次非常感谢。
ltc_mouse 2008-03-03
  • 打赏
  • 举报
回复
lz的main函数是这样的吗?

int main()
{
char a[100]="jump the computer";
char *b=strtok(a," ");
while(b!=NULL)
{
b=strtok(NULL," "); ///注意这里最后一次将返回b=NULL,而在piglatin中对*b返回,非法...
piglatin(b);
}
return 0;
}
hahaysw 2008-03-03
  • 打赏
  • 举报
回复
可以说清楚点不?piglatin(b);//输出umpjay
cout < <b;//输出jump到这都没问题
我只想证明b没被修改,这样后面运用函数strtok时应该没错,
但是程序还是运行不了~
p0303230 2008-03-03
  • 打赏
  • 举报
回复
错了
不好意思

umpjay
jump

strtok理解错误
p0303230 2008-03-03
  • 打赏
  • 举报
回复
the computertay
p0303230 2008-03-03
  • 打赏
  • 举报
回复
[Quote=引用楼主 hahaysw 的帖子:]
#include <iostream.h> 
#include <string.h>
void piglatin(char *a)
{
char *b=a;
char c=*b;
char *e="ay";
char d[20]="";
char *f=d;
for(int z=0;*b!='\0';b++,z++)
{
if(*(b+1)!='\0')
d[z]=*(b+1);
else
d[z]=c;
}
strcat(d,e);
cout < <d < <'\n';
}
int main()
{
char a[100]="jump the computer";
char *b=strtok(a," ");
piglatin(b); //输…
[/Quote]

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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