Answer:
I am writing a Python and JAVA program. Let me know if you want the program in some other programming language:
def Reverse(line):
stack = []
l = line.split()
for word in l:
stack.append(word);
while (len(stack)) :
print(stack.pop(),end= " ");
sentence= input("Enter a line of text: ")
Reverse(sentence);
Step-by-step explanation:
I will explain the program line by line.
def Reverse(line): This is the function definition of Reverse method that takes a line of text as parameter and display the words of the line in reverse order using a stack.
stack = [] In this line a character array stack is created
l= line.split() The line is split into a list of words using split() method which breaks the line into a list of words and stores them in l. This means that the line is tokenized into words.
for word in l This for loop iterates through each word in l and push that word in the stack.
stack.append(word) push every word into stack using append() method
while (len(stack)) This while loop keeps iterating and getting the words in the reverse order
print(stack.pop(),end= " ") This statement pops and prints the words in the stack in reverse order.
sentence= input("Enter a line of text: ") This statement prompts user to enter a input line of text
Reverse(sentence); calls Reverse method by passing that input sentence to the method in order to reverse the words of the sentence.
JAVA program:
import java.util.Arrays; // used to create and access arrays
import java.util.Stack; // used to implement stack data structure
import java.util.Scanner; // used to take input from user
class ReverseSentence{ // class to reverse a line of text
static void Reverse(String line) { //method Reverse that takes a line as parameter and reverses the words of the line
Stack<String> stack = new Stack<>(); // creates a Stack of strings
String[] list = line.split(" "); //splits the line/sentence into words and store these words into an array named as list
for(int i = 0; i < list.length; i++){ // loop iterates through each word of the line which is stored in list array until the end of the list is reached
stack.push(list[i]);} // using the above loop each word in the list is pushed to the stack using push method of Stack
while (!stack.empty()){ //this while loop checks if the stack is not empty and it stops when the stack is empty
System.out.print(stack.peek() + " "); /*this prints the words that were pushed into the stack, in reverse order. peek() is used to get each word in the stack present at the top.
stack.pop(); } } //use pop method of Stack to pop words from the stack
public static void main(String[] args) { //start of main function body
Scanner input= new Scanner(System.in); //create Scanner class object
System.out.print("Enter a line of text: "); //prompts user to enter a line of text
String sentence= input.nextLine(); //reads input sentence from user
Reverse(sentence); } } //calls Reverse method by passing that input sentence to reverse the words in the sentence
For example if the line of text is "how are you" then this line is first split into words how are you and then these words are pushed to stack using push() method. First the word how is pushed, then are is pushed, then the word you is pushed into stack. Next the peek() retrieves each word of the stack from top. This means first the word you is fetched, then the word are is fetched and then the word how is fetched. pop() function then pops these words from the stack. First the word you is popped, then are and then how. So this is how the line of text is printed in reverse.