字符串输出问题2

hahaysw hahaysw 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#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;
}
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
hahaysw
C++ 语言 65,214 成员 250,514 内容