From 94659a0b1915d7a6ca2ac971a05e3a27ebb0513d Mon Sep 17 00:00:00 2001 From: Gaurang Biyani <108536919+gaurangbiyani-2021@users.noreply.github.com> Date: Sun, 22 Oct 2023 15:19:11 +0530 Subject: [PATCH] issue resolved --- JAVA/ReverseStringUsingStack.java | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 JAVA/ReverseStringUsingStack.java diff --git a/JAVA/ReverseStringUsingStack.java b/JAVA/ReverseStringUsingStack.java new file mode 100644 index 0000000..a8da26a --- /dev/null +++ b/JAVA/ReverseStringUsingStack.java @@ -0,0 +1,32 @@ +import java.util.*; +public class code +{ + public static void main(String[] args) { + System.out.println(reverseString("hacktoberfest")); + } + + public static String reverseString(String str){ + + // Initialize an empty stack. + Stack st = new Stack<>(); + + // Traverse the given string from left to right + for(char ch : str.toCharArray()){ + + // Push the current character to the stack. + st.push(ch); + } + + // Initialize an empty StringBuilder + StringBuilder reverseStr = new StringBuilder(); + + // Pop from the stack one by one. + while(!st.isEmpty()){ + + // Append the current character at the last. + reverseStr.append(st.pop()); + } + + return reverseStr.toString(); + } +} \ No newline at end of file