Program to reverse a string in java
Ratik Mahajan
Posted on February 21, 2024
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();
}
π πͺ π
π©
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
javascript Built an AI tool to help my team tweet smart β goodbye manual posting, hello viral hits!π«‘π
November 28, 2024