How to Reverse a String in Java

In this post we’ll see how to reverse a string in Java. Though StringBuffer and StringBuilder classes in Java have a reverse() method that can be used to reverse a String but you may be asked to write your own logic to reverse a String. In that case too you can do it by iteration or by recursion. All the three solutions are given here.

Using StringBuilder/StringBuffer class

Output

Using iterative method to reverse a String

If you are using iterative method to reverse a string in Java then you need to loop the String starting from the end and keep adding each character to a String.

Output

Using recursive method to reverse a String

Output

That’s all for the topic How to Reverse a String in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

One Comment

  1. Pingback: How to Split a String in Java - KnpCode

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.