Last active
January 24, 2016 15:39
-
-
Save tiggreen/971a6b53227f83244878 to your computer and use it in GitHub Desktop.
Misht Community #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <algorithm> | |
#include <string> | |
using namespace std; | |
void reverseWordOrder(string::iterator begin, string::iterator end){ | |
// reverse string by chars | |
reverse(begin, end); | |
// now, reverse the words | |
string::iterator i = begin, j = begin; | |
while(true) { | |
if(j == end) { | |
reverse(i, j); | |
return; | |
} | |
if(*j == ' ') { | |
reverse(i, j); | |
i = ++j; | |
} else { | |
++j; | |
} | |
} | |
} | |
int main() { | |
string s; | |
getline(cin, s); | |
reverseWordOrder(s.begin(), s.end()); | |
cout << s << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment