Created
February 6, 2013 22:01
Revert words in a sentence (using stack).
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 <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