Vous êtes sur la page 1sur 3

9/30/2017 Reverse words of a string using stack ~ Programming Tutorials by SourceTricks

Programming Tutorials by SourceTricks


(http://www.sourcetricks.com/)
7:42:00 PM SourceTricks (https://www.blogger.com/profile/17421383822093860960)
Programming interview questions and answers (http://www.sourcetricks.com/search/label/Programming%20interview%20questions%20and%20answers)
2 comments (http://www.sourcetricks.com/2012/07/reverse-words-of-string-using-stack.html#comment-form)

Reverse words of a string using stack (http://www.sourcetricks.com/2012/07/reverse-words-of-string-using-


stack.html)

Write a program to reverse words of a string using a stack


The simplest approach to reverse words of a string is using a stack. To understand more about stack data structure refer C++ Stacks
(http://www.sourcetricks.com/2008/07/c-stacks.html). Detect end of words and push into the stack. Then parse the contents of stack and print to get the
string reversed.
C++ program to reverse words of a string using a stack

#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:

Shuffle a pack of cards (http://www.sourcetricks.com/2012/07/shuffle-pack-of-cards.html)


Write a program to shuffle a pack of cards The approach:- Initialize a cards array and set the seed for rand based on time. For each array index generate a … Read
More (http://www.sourcetricks.com/2012/07/shuffle-pack-of-cards.html)

Find the first occurrence of unique character in a string (http://www.sourcetricks.com/2013/12/find-first-occurrence-of-unique.html)


Write a program to find the first occurrence of a unique character in a string For example, given a string: "aBcBcaAbBaAdAc", b and d are unique occurrences a…
Read More (http://www.sourcetricks.com/2013/12/find-first-occurrence-of-unique.html)

Find the longest palindrome in a string (http://www.sourcetricks.com/2012/07/find-longest-palindrome-in-string.html)


Write a program to find the longest palindrome in a string The approach:- From each character location, compare the left and right locations for equality. R… Read More
(http://www.sourcetricks.com/2012/07/find-longest-palindrome-in-string.html)

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

Find if two strings are anagrams (http://www.sourcetricks.com/2012/07/find-if-two-strings-are-anagrams.html)


Write a program to find if two strings are anagrams Anagram - Wikipedia An anagram is a type of word play, the result of rearranging the letters of a word or … Read
More (http://www.sourcetricks.com/2012/07/find-if-two-strings-are-anagrams.html)

Compute the endianness (http://www.sourcetricks.com/2012/07/compute-endianness.html)


Write a program to compute the endianness of a system Endianess In computing, the term endian or endianness refers to the ordering of individually addressable…
Read More (http://www.sourcetricks.com/2012/07/compute-endianness.html)

← Newer Post (http://www.sourcetricks.com/2012/07/reverse-words-of-string-without-using.html)


Older Post → (http://www.sourcetricks.com/2012/07/find-longest-palindrome-in-string.html)

2 comments :

Employee Referral (http://www.careeref.com) March 6, 2014 at 8:13 AM (http://www.sourcetricks.com/2012/07/reverse-words-of-string-using-stack.html?


showComment=1394073808215#c8446655376190656919)

FYI, the string parsing part can be simplified:

stringstream ss;
ss << "This is a test";
string w;
while (ss >> w)
{
s.push(w);
}

Reply

Jian Zhuo (https://www.blogger.com/profile/06468977099257039433) January 10, 2017 at 3:26 PM (http://www.sourcetricks.com/2012/07/reverse-words-of-string-


using-stack.html?showComment=1484042162973#c3618386010278263982)

converse trainers (http://www.conversetrainer.co.uk)


canada goose jackets (http://www.canadagoosejacketsuk.co.uk)
ugg boots clearance (http://www.cheapuggsonsale.us.com)
ghd hair straighteners (http://www.ghd-straighteners.org.uk)
cheap jordans free shipping (http://www.cheapjordans.com.co)
replica watches (http://www.rolex-replicawatches.com.co)
beats by dre (http://www.beatsbydrdre.me.uk)
pandora charms sale (http://www.pandoracharms.name)
michael kors handbags (http://www.michaelkorshandbagsuk.me.uk)
canada goose outlet (http://www.canadagoose-outlet.net.co)
20170110

Reply

Enter your comment...

Comment as: Unknown (Goo Sign out

Publish Preview Notify me

(https://www.blogger.com/comment-iframe.g?blogID=7748177500667831327&postID=4179874911971381863&blogspotRpcToken=4461446)

Subscribe to: Post Comments ( Atom ) (http://www.sourcetricks.com/feeds/4179874911971381863/comments/default)

Tutorial Pages

Java Tutorials (http://www.sourcetricks.com/p/java-tutorials.html)

Micro Services (http://www.sourcetricks.com/p/microservices.html)

Java Interview Questions (http://www.sourcetricks.com/2014/06/core-java-interview-questions.html)

Scala Tutorials (http://www.sourcetricks.com/p/scala-tutorials.html)

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

Programming in C++ (http://www.sourcetricks.com/p/programming-in-c.html)

Programming interview questions and answers in C++ (http://www.sourcetricks.com/p/programming-interview-questions-and.html)

Data Structures using C++ (http://www.sourcetricks.com/p/data-structures-using-c.html)

Algorithms in C++ (http://www.sourcetricks.com/p/algorithms-in-c.html)

Design Patterns using C++ (http://www.sourcetricks.com/p/design-patterns-using-c.html)

Android (http://www.sourcetricks.com/p/android.html)

HTML, CSS and Javascript (http://www.sourcetricks.com/p/html-css-and-javascript.html)

UML Notations (http://www.sourcetricks.com/2008/05/uml-generalization.html)

Tag Cloud

Java (http://www.sourcetricks.com/search/label/Java) CPP


(http://www.sourcetricks.com/search/label/CPP) Programming interview questions and
answers
(http://www.sourcetricks.com/search/label/Programming%20interview%20questions%20and%
20answers) Design Patterns (http://www.sourcetricks.com/search/label/Design%20Patterns) Scala
(http://www.sourcetricks.com/search/label/Scala) Android (http://www.sourcetricks.com/search/label/Android) Algorithms
(http://www.sourcetricks.com/search/label/Algorithms) Data Structures (http://www.sourcetricks.com/search/label/Data%20Structures) Micro Services
(http://www.sourcetricks.com/search/label/Micro%20Services) JavaScript (http://www.sourcetricks.com/search/label/JavaScript) tools
(http://www.sourcetricks.com/search/label/tools) UML (http://www.sourcetricks.com/search/label/UML) html (http://www.sourcetricks.com/search/label/html)

Faceplusplus Cognitive

Detect, compare and search human


faces, locate keypoints of face
components

Sourcetricks
580 likes

Like Page Share

Be the first of your friends to like this

Follow @sourcetricks 53 followers

http://www.sourcetricks.com/2012/07/reverse-words-of-string-using-stack.html#.Wc-m92iCzIU 3/3

Vous aimerez peut-être aussi