Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

FREQUENTLY ASKED JAVA PROGRAMS 35 – Java Program to Reverse a String Using Stack

Posted on 02/19/2025 By admin

Hello Folks,

As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced, in this post we will see a java program to reverse a string using stack.

Before we go for definition of Stack, let’s consider some examples:-

Suppose you have X number of plates. You organise them as one at the top of another. So the plate you kept first is in at the bottom and so on. Last plate will be on top. If you want a plate, obviously you should take plate which is in top ( Which was kept in last while organising ). We can summarize this as “Last In First Out (LIFO)”. The organised structure of plate is called a Stack.

Stack of plain white dinner plates, on white background

Stack is a data structure which works based on “Last In First Out (LIFO)” principal. There are mainly two points about stack:-

  1. New element will be added always on top.
  2. Element will be deleted only from top. You can not delete in between.

In Java, we have a Stack class which follows LIFO principal. It has two important method push() and pop().

push() – Pushes an item onto the top of this stack.

pop() – Removes the object at the top of this stack and returns that object as the value of this function.

  1. Create an empty stack of Character.
  2. Convert given string to character array.
  3. Iterate char array from beginning and add to stack one by one.
  4. Now iterate stack till it is empty and create a string.
  5. You have reverse string now.

Java Program:-

package StringPrograms; import java.util.Stack; public class ReverseStringUsingStack { public static void main(String[] args) { // Given string to reverse String stringToReverse = "MakeSeleniumEasy"; // Convert string to char array char charArray[]= stringToReverse.toCharArray(); // Creating an empty Stack Stack charConatiner= new Stack(); // Add characters of char array to Stack for(Character c: charArray) { charConatiner.push(c); } // Creating an empty string String reverseString =""; // Iterating Stack and creating string while(!charConatiner.isEmpty()) { reverseString = reverseString + String.valueOf(charConatiner.pop()); } System.out.println("Given String : "+reverseString); System.out.println("Reversed String : "+reverseString); } } 

#HappyCoding

Uncategorized

Post navigation

Previous Post: REST Assured Tutorial 69 – Introduction To JsonAssert Library
Next Post: Core Java Quiz – 1 – Variables

Related Posts

Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
Log4j2 Tutorial 7 – Working Mechanism of Default Rollover Strategy Uncategorized
How To Verify Text In Bold Using Selenium WebDriver Uncategorized
Git Tutorial 31 – How To Retrieve Deleted But Not Committed File in Git? Uncategorized
InnerClassMissing – Make Selenium Easy Uncategorized
Create a Map From Excel Data in Java – Selenium – API Automation Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark