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

翻转一个句子中的单词  比如输入 this is a test  输出  test a is this 输入foobar  输出foobar

复制代码
 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 }
复制代码

 

posted @   xiaoxi666  阅读(303)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
TOP
点击右上角即可分享
微信分享提示