Java Program to Reverse a String(5 ways)

In this post, we will write a Java Program to reverse a given String. We show you 5 different ways we can reverse the String.

5 Methods to Reverse a String

1. Reverse With String Concat

public static final String reverseWithStringConcat(String string) {
 String output = new String();
 for (int i = (string.length() - 1); i >= 0; i--) {
  output += (string.charAt(i));
 }
 
 display(string,output);
 return output;
}

2. Reverse With StringBuilder

public static final String reverseWithStringBuilder(String string) {
 final StringBuilder builder = new StringBuilder();
 for (int i = (string.length() - 1); i >= 0; i--) {
  builder.append(string.charAt(i));
 }
 display(string,builder.toString());
 return builder.toString();
}

3. Reverse With StringBuilder Builtin Method - reverse()

public static final String reverseWithStringBuilderBuiltinMethod(String string) {
 final StringBuilder builder = new StringBuilder(string);
 display(string, builder.reverse().toString());
 return builder.reverse().toString();
}

4. Reverse With Swapping characters

public static final String reverseWithSwaps(String string) {
 final char[] array = string.toCharArray();
 final int length = array.length - 1;
 final int half = (int) Math.floor(array.length / 2);

 char c;
 for (int i = length; i >= half; i--) {
  c = array[length - i];
  array[length - i] = array[i];
  array[i] = c;
 }
 display(string, String.valueOf(array));
 return String.valueOf(array);
}

5. Reverse with XOR(^) operator

public static final String reverseWithXOR(String string) {
 final char[] array = string.toCharArray();
 final int length = array.length;
 final int half = (int) Math.floor(array.length / 2);

 for (int i = 0; i < half; i++) {
  array[i] ^= array[length - i - 1];
  array[length - i - 1] ^= array[i];
  array[i] ^= array[length - i - 1];
 }
 display(string, String.valueOf(array));
 return String.valueOf(array);
}

Complete Program

package com.javaguides.strings.examples;

/**
 * 5 ways to reverse a String in Java
 * @author javaguides.net
 *
 */
public class ReverseString5Ways {

 public static void main(String[] args) {
  reverseWithStringConcat("javaguides");
  reverseWithStringBuilder("javaguides");
  reverseWithStringBuilderBuiltinMethod("javaguides");
  reverseWithSwaps("javaguides");
  reverseWithXOR("javaguides");
 }

 public static final String reverseWithStringConcat(String string) {
  String output = new String();
  for (int i = (string.length() - 1); i >= 0; i--) {
   output += (string.charAt(i));
  }
  
  display(string,output);
  return output;
 }

 public static final String reverseWithStringBuilder(String string) {
  final StringBuilder builder = new StringBuilder();
  for (int i = (string.length() - 1); i >= 0; i--) {
   builder.append(string.charAt(i));
  }
  display(string,builder.toString());
  return builder.toString();
 }

 public static final String reverseWithStringBuilderBuiltinMethod(String string) {
  final StringBuilder builder = new StringBuilder(string);
  display(string, builder.reverse().toString());
  return builder.reverse().toString();
 }

 public static final String reverseWithSwaps(String string) {
  final char[] array = string.toCharArray();
  final int length = array.length - 1;
  final int half = (int) Math.floor(array.length / 2);

  char c;
  for (int i = length; i >= half; i--) {
   c = array[length - i];
   array[length - i] = array[i];
   array[i] = c;
  }
  display(string, String.valueOf(array));
  return String.valueOf(array);
 }

 public static final String reverseWithXOR(String string) {
  final char[] array = string.toCharArray();
  final int length = array.length;
  final int half = (int) Math.floor(array.length / 2);

  for (int i = 0; i < half; i++) {
   array[i] ^= array[length - i - 1];
   array[length - i - 1] ^= array[i];
   array[i] ^= array[length - i - 1];
  }
  display(string, String.valueOf(array));
  return String.valueOf(array);
 }

 
 private static final void display(String original , String reverse){
  System.out.println(original);
  System.out.println(reverse);
  System.out.println("----------------------------");
 }
}
Output:
javaguides
sediugavaj
----------------------------
javaguides
sediugavaj
----------------------------
javaguides
sediugavaj
----------------------------
javaguides
sediugavaj
----------------------------
javaguides
sediugavaj
----------------------------
From above 5 different approaches , prefer StringBuilder built-in reverse() method for better performance.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Related String Programs

Note that these programs are asked in interviews.

Comments