• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
翻转一个句子中的单词  比如输入 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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
小程序日期时间段选择发布时间:2022-07-18
下一篇:
记事本小程序完成啦!发布时间:2022-07-18
热门推荐
    热门话题
    阅读排行榜

    扫描微信二维码

    查看手机版网站

    随时了解更新最新资讯

    139-2527-9053

    在线客服(服务时间 9:00~18:00)

    在线QQ客服
    地址:深圳市南山区西丽大学城创智工业园
    电邮:jeky_zhao#qq.com
    移动电话:139-2527-9053

    Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap