How to Reverse a String in Java (5 Ways)

String reversal is changing the characters position from last to first in string.

There are different ways to to achieve reversal.

We will see them one by one.

  • Using Loops (or Iterations)
  • Usnig StringBuffer or StringBuilder ‘s reverse()
  • Using toCharArray() of Array
  • Using Recursion
  • Using Collections

Lets see different ways one by one

1 Reverse a string Using Loops

Following programs will show how to use loops to reverse a string.

Using loop we extracted one one character from string using charAt() of string to extract a character and assigned to another String or StringBuffer or in StringBuilder.

This program is also helpful in String reverse in java without using inbuilt function and in reverse a string without using string function in java

Below are the simple java programs for beginners

A. Reverse a string in java using for loop

  1. Create a String object String string = "Programmer";
  2. Create a for loop to iterate it from string object length to 0 for (int i = string.length() - 1; i>= 0; i--)
  3. Extract each character by using charAt() and assign it to another String object.

We run here java reverse for loop to get each characters and assigned to another string.

Printed the reversedString to get reverse string in java program.

B. Reverse a string in java using while loop

String object is inmutable so changing object value is not possible to avoid this we used StringBuffer here

C. Reverse a string in java using do while loop

Here StringBuilder is used to reverse the string

2. Using StringBuffer or StringBuilder classes

StringBuffer and StringBuilder provides a method reverse() to reverse the string.

This is very simple approach any one can easily reverse string using this approach.

Using this method we can reverse string.

3. Using toCharArray() of Array

toCharArray() is used to convert String Object to character array.

We follow below steps to revers string using toCharArray().

  1. Take a String
  2. Conver it to Character array string.toCharArray();
  3. Reverse loop in character array
  4. Assing it to a string object

4. Using Recursion to reverse a String

Recursion is one of the common way to reverse a string.

In many java interviews, it also asked to reverse a string without using iteration and reverse a string in java without using inbuilt function . In that case we use recursion.

  1. Create a method to call itself reverseString(String s)
  2. If String is empty then return
  3. If not empty make recursion call to reverse a string reverseString(s.substring(1)) + s.charAt(0);

5. Using Collections to Reverse a String in Java

  1. Get String
  2. Conver to List

List charList = string.chars()
.mapToObj(e -> (char) e)
.collect(Collectors.toList());

  1. Reverse List Collections.reverse(charList);
  2. Convert to String String reverseString = charList.stream().map(String::valueOf).collect(Collectors.joining());

Above program is a good example of  Java 8 using Streams