Skip to content

Instantly share code, notes, and snippets.

@tiggreen
Last active January 24, 2016 15:39
Show Gist options
  • Save tiggreen/971a6b53227f83244878 to your computer and use it in GitHub Desktop.
Save tiggreen/971a6b53227f83244878 to your computer and use it in GitHub Desktop.
Misht Community #1
#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