Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Reverse a String Using Stacks in Java



In this article, we will understand how to reverse a string using stacks. String is a datatype that contains one or more characters and is enclosed in double quotes(" "). The stack is a linear data structure that is used to store the collection of objects. It is based on Last-In-First-Out (LIFO).

Problem Statement

Given a string write a program in Java to reverse a string using stacks. Below is a demonstration of the same ?

Input

Input string: Java Program

Output

Reversed string: margorP avaJ

Different Approaches

 Below are the different approaches to reverse a string using stacks ?

Reverse a string using stacks in the Main method

Following are the steps to reverse a string using stacks in the Main method ?

  • First, import all the classes from the java.util package.
  • We will define the input string.
  • Convert the string into a character array.
  • Push each character into the stack.
  • Pop characters from the stack to reverse the string.
  • Display the reversed string.

Example

Here, we bind all the operations together under the ?main' function.

import java.util.*;
public class ReverseString {
   public static void main(String[] args) {
      System.out.println("Required packages have been imported");
      String input_string = "Java Program";
      System.out.println("The string is defined as " +input_string);
      char[] reverse = new char[input_string.length()];
      Stack<Character> stack = new Stack<Character>();
      for (int i = 0; i < input_string.length(); i++) {
         stack.push(input_string.charAt(i));
      }
      int i = 0;
      while (!stack.isEmpty()) {
         reverse[i++] = stack.pop();
      }
      String result = new String(reverse);
      System.out.println("\nThe reversed string is: " + result);
   }
}

Output

Required packages have been imported
The string is defined as Java Program

The reversed string is: margorP avaJ

Code Explanation

In this approach, all operations are handled within the main method. We start by importing the Stack class from the java.util package, which provides the stack data structure. The input string "Java Program" is defined, and we convert it into a character array. Using a for loop, each character is pushed onto the stack. As the stack follows LIFO, popping elements from it will yield the characters in reverse order, which are stored in a new character array. Finally, the reversed string is created using the String constructor and displayed.

Reverse a string using stacks by encapsulating operations

Following are the steps to reverse a string using stacks by encapsulating operations

  • Import necessary classes from the java.util package.
  • Define a method reverse_string() to handle the string reversal.
  • Push each character of the string into the stack within the method.
  • Pop characters from the stack to reverse the string and return the result.
  • Use the reverse_string() method in the main function to reverse the string and display the result.

Example

Here, we encapsulate the operations into functions exhibiting object oriented programming.

import java.util.*;
public class ReverseString {
   public static String reverse_string(String input_string) {
      char[] reverse = new char[input_string.length()];
      Stack<Character> stack = new Stack<Character>();
      for (int i = 0; i < input_string.length(); i++) {
         stack.push(input_string.charAt(i));
      }
      int i = 0;
      while (!stack.isEmpty()) {
         reverse[i++] = stack.pop();
      }
      return new String(reverse);
   }
   public static void main(String[] args) {
      System.out.println("Required packages have been imported");
      String input_string = "Java Program";
      System.out.println("The string is defined as " +input_string);
      System.out.println("\nThe reversed string is: " + reverse_string(input_string));
   }
}

Output

Required packages have been imported
The string is defined as Java Program

The reversed string is: margorP avaJ

Code Explanation

In this approach, we encapsulate the string reversal logic in a separate method called reverse_string(). This method follows the same steps as in Example 1: it pushes each character of the input string onto a stack and then pops them to reverse the string. The reverse_string() method returns the reversed string, which is then displayed in the main function. This structure enhances code reusability and demonstrates the principles of OOP.

Updated on: 2024-08-22T11:59:20+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements