
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reverse a String Using Stacks in Java
Stack is a linear data structure where we can store elements. It uses the LIFO (last in, first out) principle, which means the item we add last will be the first one to be removed. In this article, we will understand how to reverse a string using stacks. Let's take an example:
Input: String input_string = "Java Program"; Output: String reversed_string = "margorP avaJ";
Ways to Reverse a String Using Stacks
Below are the different approaches to reverse a string using stacks:
Reverse a string using stacks in the Main method
The following are the steps to reverse a string using built-in stack methods
- Declare a string "Java Program".
- Initialize a character array of the same length as the string.
- Create a stack of characters.
- Use a for loop to iterate through the string and push each character onto the stack.
- Run a while loop until the stack is empty.
- Pop each character from the stack and store it in the character array.
Example
Here is an example where we reverse a string using built-in stack methods.
import java.util.*; import java.util.Stack; public class ReverseString { public static void main(String[] args) { String s = "Java Program"; char[] reverse = new char[s.length()]; int i = 0; Stack<Character> stack = new Stack<Character>(); for(char c : s.toCharArray()) { stack.push(c); } while(!stack.isEmpty()) { reverse[i++] = stack.pop(); } System.out.println("The original string is defined as: " + s); System.out.println("\nThe reversed string is: " + new String(reverse)); } }
Output
Required packages have been imported The string is defined as Java Program The reversed string is: margorP avaJ
Reverse a String using a Custom Stack
In this approach, we will create a custom stack class to reverse a string. We will be using an array to implement the stack.
Algorithm
- Declare a class CustomStack with the following methods:
- push(char c): To add a character to the stack.
- pop(): To remove and return the top character from the stack.
- isEmpty(): To check if the stack is empty.
- size(): To return the current size of the stack.
- In the main method:
- Initialize a string to be reversed.
- Create an instance of CustomStack.
- Use a for loop to iterate through the string and push each character onto the custom stack.
- Run a while loop until the stack is empty.
- Pop each character from the stack and append it to a StringBuilder.
- Print the original and reversed strings.
Example
Here is an example where we reverse a string using a custom stack.
import java.util.EmptyStackException; public class ReverseString { private static class CustomStack{ private char[] stack; private int top; private int capacity; public CustomStack(int size) { stack = new char[size]; capacity = size; top = -1; } public void push(char c){ if(top == capacity - 1){ throw new StackOverflowError("Stack is full"); }else { stack[++top] = c; } } public char pop(){ if(isEmpty()){ throw new EmptyStackException(); }else { return stack[top--]; } } public boolean isEmpty(){ return top == -1; } public int size(){ return top + 1; } } public static void main(String[] args){ String s = "Java Program"; CustomStack stack = new CustomStack(s.length()); for(char c : s.toCharArray()) { stack.push(c); } StringBuilder reversed = new StringBuilder(); while(!stack.isEmpty()) { reversed.append(stack.pop()); } System.out.println("The original string is defined as: " + s); System.out.println("\nThe reversed string is: " + reversed.toString()); } }
Output
Following is the output of the above code:
The original string is defined as: Java Program The reversed string is: margorP avaJ
Advertisements