Program to reverse a string in java

ranujmahajan

Ratik Mahajan

Posted on February 21, 2024

Program to reverse a string in java

Reverse a String in java Explanation : pseudo Code

  1. Convert the string to array of characters.
  2. add all the characters to the stack
  3. create an object of the StringBuilder class and keep on adding the stack characters to the StringBuilder object.
  4. convert string builder to the string.
 public static String reverseString(String word) {
        Stack<Character> charStack = new Stack<>();
        for (char ch : word.toCharArray()) {
            charStack.push(ch);
        }
        StringBuilder sbString = new StringBuilder();
        while (!charStack.isEmpty()) {
            sbString.append(charStack.pop());
        }
        return sbString.toString();
    }
Enter fullscreen mode Exit fullscreen mode
πŸ’– πŸ’ͺ πŸ™… 🚩
ranujmahajan
Ratik Mahajan

Posted on February 21, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related