字符串输出问题2
#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;
}
#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;
}
...全文
相关推荐
PHP程序设计-3期(KC016) 2.8.8字符串输出 常见问题.docx
**问题 2:print 和 echo 的差异**`print` 和 `echo` 都是用来输出字符串的,它们在大多数情况下表现相似,都是没有返回值的语句。
Python输出指定字符串的方法
` 是要输出的字符串,放在`print()`函数的括号内。在给定的问题描述中,我们需要寻找一种特定的日期格式,即由四个数字代表年份,后跟月份名称的字符串,例如 "2019Nov"。
Python字符串逆序输出的实例讲解
对于字符串`str_a = "1a2b3c4d5e6f"`,我们可以用以下方式获取奇数和偶数位置的字符:```python# 输出奇数位的字符for i in str_a[::2]: print(i)#
简单讲解Python中的字符串与字符串的输入输出
"本文主要介绍了Python中的字符串处理,包括字符串的定义、转义字符的使用、多行字符串的表示以及编码问题。同时,也展示了如何进行字符串的输出,并简要提及了Python2与Python3在编码处理上
递归实现字符串反向输出
**宏定义**: ```c #define N 100 ``` 这里定义了一个常量`N`,用于指定字符串的最大长度为100个字符。2.


