Ratik Mahajan Posted on Feb 21 Program to reverse a string in java #programming Reverse a String in java Explanation : pseudo Code Convert the string to array of characters. add all the characters to the stack create an object of the StringBuilder class and keep on adding the stack characters to the StringBuilder object. 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 Top comments (2) Subscribe Personal Trusted User Create template Templates let you quickly answer FAQs or store snippets for re-use. Submit Preview Dismiss Collapse Expand Mr. Linxed Mr. Linxed Mr. Linxed Follow 👨💻 Professional problem solver - 🎧 Music lover Hello! Thanks for checking out my profile. If you haven't yet, make sure to also follow me on my other social media! ⬇️ Location The Netherlands Education The World Wide Web Work Professional Problem Solver Joined Sep 2, 2022 • Feb 21 Dropdown menu Copy link Hide StringBuilder has a reverse method you could utilize, since you're already using StringBuilder. StringBuilder input = new StringBuilder(); input.append(input); input.reverse(); Enter fullscreen mode Exit fullscreen mode Collapse Expand Ratik Mahajan Ratik Mahajan Ratik Mahajan Follow Joined May 28, 2021 • Feb 22 Dropdown menu Copy link Hide stringBuilder reverse would also loop on the elements on the stack. i used string builder to not create many string pools in the java. Code of Conduct • Report abuse Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse
Top comments (2)
StringBuilder has a reverse method you could utilize, since you're already using StringBuilder.
stringBuilder reverse would also loop on the elements on the stack. i used string builder to not create many string pools in the java.