zoukankan      html  css  js  c++  java
  • 【模板小程序】翻转一个句子中的单词

    翻转一个句子中的单词  比如输入 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 }
    『注:本文来自博客园“小溪的博客”,若非声明均为原创内容,请勿用于商业用途,转载请注明出处http://www.cnblogs.com/xiaoxi666/』
  • 相关阅读:
    使用Mockito进行单元测试【2】—— stub 和 高级特性[转]
    使用Mockito进行单元测试【1】——mock and verify[转]
    一个更加简洁的 建造者模式
    建造者模式
    另一种 模板模式
    模板模式
    适配器模式的另一种解释
    适配器模式
    迭代器模式
    迭代器模式 循环数组
  • 原文地址:https://www.cnblogs.com/xiaoxi666/p/7272857.html
Copyright © 2011-2022 走看看