We need to write a java program to reverse string and output it. Here, string can be a single word or a sentence.


A String in Java can be reversed in many ways. Let’s see each of the methods one by one.

Steps to Reverse a String in Java

  1. Take input a string (str).
  2. Loop (i) through (str) from back to front.
  3. Concatenate the character at position (i) of (str) to (rev).
  4. Output (rev).

Reverse String in Java using For Loop

Note: use nextLine() instead of next() to take sentence as input.


Reverse String in Java using While Loop

In this method, we are using the same approach as the first java program but instead of for loop, we are using while loop. The loop starts from last index of the input string (i.e str.length()-1) and will end on 0.


Reverse String in Java Without Loop

Java has StringBuilder class which has inbuilt reverse() method, which can reverse the string without any loops. For this we have to create StringBuilder object by passing input string as parameter.


Choose whichever java program works best for you to reverse a string. If you like then share and if you have any doubts or suggestion then please comment below.

Leave a Reply