Java Program to Reverse a String

Write a Java program to reverse a string with an example. We can do this in multiple ways:

  • Using StringBuilder function.
  • Using for loop
  • while loop
  • Using Temporary Variables to swap
  • Using StringBuffer
  • Recursive Functions

In this Java example, we are using the StringBuilder function to reverse the given string.

import java.util.Scanner;

public class Example {	
	
	private static Scanner sc;	
	
	public static void main(String[] args) 
	{
		String revStr;

		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter Text =  ");
		revStr = sc.nextLine();
		
		StringBuilder sb = new StringBuilder(revStr);
		
		System.out.println("The result =  " + sb.reverse());
	}
}
Java Program to Reverse a String 1

In this example, we used for loop to iterate revStr from end to start in reverse order. Next, we are concat each character to the newStr.

import java.util.Scanner;

public class Example2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String revStr;
		int i;

		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter =  ");
		revStr = sc.nextLine();
		
		String newStr = "";
		
		for(i = revStr.length() - 1; i >= 0; i--) {
			newStr += revStr.charAt(i);
		}		
		System.out.println("The result =  " + newStr);
	}
}
Please Enter =  text revrse
The result =  esrver txet

In this Java string reverse code, first, we converted the revStr to revCharArr character array. Next, we used for loop to traverse revCharArr from right to left, assigned the last value to the first index, etc.

import java.util.Scanner;

public class Example3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String revStr;
		int i, j = 0;

		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter Text =  ");
		revStr = sc.nextLine();
		
		char[] revCharArr = revStr.toCharArray();
		
		System.out.print("\nThe result =  " );
		for(i = revCharArr.length - 1; i >= 0; i--) {
			revCharArr[j++] = revStr.charAt(i);
		}		
		System.out.print(revCharArr);
	}
}
Please Enter Text =  Hello

The result =  olleH

Java Program to Reverse a String using While Loop

import java.util.Scanner;

public class Example4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String revStr;

		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter =  ");
		revStr = sc.nextLine();
		
		char[] revCharArr = revStr.toCharArray();
		String newSt = "";
		int i = revCharArr.length - 1; 
		
		System.out.print("\nThe first result  =  " );
		while(i >= 0) {
			
			System.out.print(revCharArr[i]);
			newSt += revCharArr[i];
			i--;
		}		
		System.out.println("\nThe Second result =  " + newSt);
	}
}
Please Enter =  Programming World

The first result  =  dlroW gnimmargorP
The Second result =  dlroW gnimmargorP

In this Java code for string reverse, we used a temporary variable to hold the values and swap them.

import java.util.Scanner;

public class Example5 {
	private static Scanner sc;
	public static void main(String[] args) {
		String revStr;
		char temp;

		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter Text =  ");
		revStr = sc.nextLine();
		
		char[] revCharArr = revStr.toCharArray();
		int len = revStr.length() - 1;
		
		System.out.print("\nThe result =  " );
		for(int i = 0; i < revCharArr.length / 2; i++) {
			temp = revCharArr[i];
			revCharArr[i] = revCharArr[len];
			revCharArr[len--] = temp;
		}		
		System.out.print(revCharArr);
	}
}
Please Enter Text =  Welcome to world

The result =  dlrow ot emocleW

Java String Reverse using StringBuffer

The Java StringBuffer has a reverse function. So, we convert the given revStr to sbuff (StringBuffer) and invert the same.

import java.util.Scanner;

public class Example6 {
	private static Scanner sc;
	public static void main(String[] args) {
		String revStr;

		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter Text =  ");
		revStr = sc.nextLine();
		
		StringBuffer sbuff = new StringBuffer(revStr);
		
		System.out.println("The result =  " + sbuff.reverse());
	}
}
Please Enter Text =  tutorial gateway
The result =  yawetag lairotut

Java String Reverse using Recursion

import java.util.Scanner;

public class Example7 {
	private static Scanner sc;
	public static void main(String[] args) {
		String revStr;

		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter Text =  ");
		revStr = sc.nextLine();
		sc.close();
		
		String rev = retxt(revStr);
		
		System.out.print("\nThe result =  " + rev );		
	}
	public static String retxt(String revStr) {
		if(revStr.isEmpty()) {
			return revStr;
		}
		return retxt(revStr.substring(1)) + revStr.charAt(0);
	}
}
Please Enter text =  Learn Program

The result =  margorP nraeL

It is an extension to the above program.

import java.util.Scanner;

public class Example8 {
	private static Scanner sc;
	public static void main(String[] args) {
		String revStr;

		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter text =  ");
		revStr = sc.nextLine();
		
		System.out.print("\nThe result =  " );
		rvstDef(revStr);
	}
	
	public static void rvstDef(String revStr) {
		if(revStr.isEmpty()) {
			System.out.println(revStr);
		}
		else {
			System.out.print(revStr.charAt(revStr.length() - 1));
			rvstDef(revStr.substring(0,revStr.length() - 1));
		}
	}
}
Please Enter text =  This Tutorial

The result =  lairotuT sihT