Reverse the Sentence using Stack

Given a string str consisting of a sentence, the task is to reverse the entire sentence word by word.

Examples:

Input: str = “geeks for geeks”
Output: geeks for geeks



Input: str = “data structures and algorithms”
Output: algorithms and structures data

Approach: An approach without using stack is discussed here. This problem can also be solved using stack by following the below steps:

  1. Create an empty stack.
  2. Tokenize the input string into words using spaces as separator with the help of strtok()
  3. Push the words into the stack.
  4. Pop the words from the stack until the stack is not empty which will be in reverse order.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to reverse the words
// of the given sentence
void reverse(char k[])
{
  
    // Create an empty character array stack
    stack<char*> s;
    char* token = strtok(k, " ");
  
    // Push words into the stack
    while (token != NULL) {
        s.push(token);
        token = strtok(NULL, " ");
    }
  
    while (!s.empty()) {
  
        // Get the words in reverse order
        cout << s.top() << " ";
        s.pop();
    }
}
  
// Driver code
int main()
{
    char k[] = "geeks for geeks";
    reverse(k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.Arrays;
import java.util.Stack;
  
class GFG 
{
      
// Function to reverse the words
// of the given sentence
static void reverse(String k)
{
  
    // Create an empty character array stack
    Stack<String> s = new Stack<>();
    String[] token = k.split(" ");
      
    // Push words into the stack
    for(int i = 0; i < token.length; i++)
    {
        s.push(token[i]);
    }
  
    while (!s.empty()) 
    {
  
        // Get the words in reverse order
        System.out.print(s.peek() + " ");
        s.pop();
    }
}
  
// Driver code
public static void main(String[] args) 
{
    String k = "geeks for geeks";
    reverse(k);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to reverse the words 
# of the given sentence 
def reverse(k):
    # Create an empty character array stack 
    s = [] 
    token = k.split()
      
    # Push words into the stack 
    for word in token :
        s.append(word);
          
    while (len(s)) :
        # Get the words in reverse order 
        print(s.pop(),end= " "); 
      
  
# Driver code 
if __name__ == "__main__"
  
    k = "geeks for geeks"
    reverse(k); 
      
# This code is contributed by AnkitRai01

chevron_right


Output:

geeks for geeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji, AnkitRai01



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.