| F.Final Ugly English | |||||
| |||||
| Description | |||||
|
ACM twist-toy encountered such a problem in the work, this article, to ensure that this article only lowercase letters and spaces, please send the articles in each word inverted output, such as "hello world this is an competition". You should output "olleh dlrow siht si na noititepmoc". | |||||
| Input | |||||
|
A group of data, each line of data input from the lower case letters and spaces of the article, the length of not more than one thousand | |||||
| Output | |||||
| Output a line for each word after the reversal of the article. | |||||
| Sample Input | |||||
| hello world this is an competition | |||||
| Sample Output | |||||
| olleh dlrow siht si na noititepmoc |
思路:这里get 到的就是reverse()函数的字符串翻转。但是在我自己打代码的时候发现了一个比较好玩的两边对称翻转,另附代码2上;
注:char 类型的字符串整行输入是gets(a);但是string 类型的整行输入是getline(cin,a);
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxx 1010
const int mod=10007;
int broken[maxx];
int num[maxx];
int main()
{
string a;
while(getline(cin,a))
{
int len=a.length();
for(int i=0;i<len;i++)
{
int j;
int l;
l=i;
for(j=l;a[j]!=' '&&j<len;j++);
i=j;
reverse(a.begin()+l,a.begin()+j);
}
cout<<a<<endl;
}
return 0;
}
代码2:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxx 1010
const int mod=10007;
int broken[maxx];
int num[maxx];
int main()
{
string a;
while(getline(cin,a))
{
int len=a.length();
for(int i=0;i<len;i++)
{
int j;
int l;
l=i;
for(j=l;a[j]!=' '&&j<len;j++);
//i=j;
reverse(a.begin()+l,a.begin()+j);
}
cout<<a<<endl;
}
return 0;
}
行结果有兴趣的可以自己运行看看。(其实是 我附不上图片
)
本文介绍了一种字符串处理问题,即如何将英文文章中每个单词进行逆序输出,并提供两种实现方式,利用C++编程语言中的reverse函数实现字符串的翻转。

被折叠的 条评论
为什么被折叠?



