前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【模板小程序】翻转一个句子中的单词

【模板小程序】翻转一个句子中的单词

作者头像
xiaoxi666
发布2018-10-29 17:32:17
5210
发布2018-10-29 17:32:17
举报
文章被收录于专栏:xiaoxi666的专栏xiaoxi666的专栏
代码语言:javascript
复制
翻转一个句子中的单词  比如输入 this is a test  输出  test a is this 输入foobar  输出foobar
代码语言:javascript
复制
 1 /*
 2 本程序说明:
 3 
 4 翻转一个句子中的单词  比如输入 this is a test  输出  test a is this 输入foobar  输出foobar
 5 
 6 思路:先翻转整个句子,再针对每一个单词翻转之
 7 
 8 */
 9 #include <iostream>
10 #include <string>
11 #include <algorithm>
12 using namespace std;
13 
14 void reverse_word(string& sentence)
15 {
16     reverse(sentence.begin(),sentence.end());
17 
18     string::iterator index_start    =   sentence.begin();
19     string::iterator index_end      =   sentence.begin();
20     for(string::iterator it=sentence.begin();it<sentence.end();++it)
21     {
22         if(' '==*it)
23         {
24             index_end=it;
25             reverse(index_start,index_end);
26             index_start=++it;
27         }
28     }
29     reverse(index_start,sentence.end());//翻转最后一个单词
30 }
31 
32 int main()
33 {
34     string sentence;
35     while(getline(cin,sentence))
36     {
37         reverse_word(sentence);
38         cout<<sentence<<endl;
39     }
40     return 0;
41 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档