I have \"Hello World\"
kept in a String variable named hi
.
I need to print it, but reversed.
How can I do this? I understand there
I tried, just for fun, by using a Stack. Here my code:
public String reverseString(String s) {
Stack stack = new Stack<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
stack.push(s.charAt(i));
}
while (!stack.empty()) {
sb.append(stack.pop());
}
return sb.toString();
}