Reverse Words of A String Using Stack - Programming Tutorials by SourceTricks
Reverse Words of A String Using Stack - Programming Tutorials by SourceTricks
#include <iostream>
#include <stack>
using namespace std;
int main() {
char input[] = "this is a test";
int sz = sizeof(input) / sizeof(char) - 1;
stack<string> s;
char word[sz];
int j = 0;
for ( int i = 0; i <= sz; i++ ) {
if ( input[i] != ' ' && input[i] != '\0' ) {
word[j++] = input[i];
}
else {
word[j++] = '\0';
s.push(word);
j = 0;
}
}
while ( ! s.empty() ) {
string w = s.top();
s.pop();
cout << w << " ";
}
}
Output:-
test a is this
Related Posts:
http://www.sourcetricks.com/2012/07/reverse-words-of-string-using-stack.html#.Wc-m92iCzIU 1/3
9/30/2017 Reverse words of a string using stack ~ Programming Tutorials by SourceTricks
2 comments :
stringstream ss;
ss << "This is a test";
string w;
while (ss >> w)
{
s.push(w);
}
Reply
Reply
(https://www.blogger.com/comment-iframe.g?blogID=7748177500667831327&postID=4179874911971381863&blogspotRpcToken=4461446)
Tutorial Pages
http://www.sourcetricks.com/2012/07/reverse-words-of-string-using-stack.html#.Wc-m92iCzIU 2/3
9/30/2017 Reverse words of a string using stack ~ Programming Tutorials by SourceTricks
Android (http://www.sourcetricks.com/p/android.html)
Tag Cloud
Faceplusplus Cognitive
Sourcetricks
580 likes
http://www.sourcetricks.com/2012/07/reverse-words-of-string-using-stack.html#.Wc-m92iCzIU 3/3