Skip to content

Instantly share code, notes, and snippets.

@KrzysztofCiba
Created February 6, 2013 22:01
Revert words in a sentence (using stack).
#include <iostream>
#include <sstream>
#include <stack>
using namespace std;
// using stack
string revertStack( string sentence ) {
stringstream inout(sentence);
string word, news;
stack<string> st;
while ( inout >> word ) {
st.push( word );
}
while ( st.size() ) {
news += " " + st.top();
st.pop();
}
return news;
}
int main() {
string sentence = "The quick brown fox jumped over the lazy dog.";
sentence = revertStack( sentence );
cout << sentence << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment