Java Program to Reverse a String

Write a Java program to reverse a string with an example. In this Java example, we are using the StringBuilder reverse function to reverse the given string.

import java.util.Scanner;

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

		sc= new Scanner(System.in);

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

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

import java.util.Scanner;

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

		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Reverse =  ");
		revStr = sc.nextLine();
		
		String newString = "";
		
		for(i = revStr.length() - 1; i >= 0; i--) {
			newString += revStr.charAt(i);
		}		
		System.out.println("The result of String Reverse =  " + newString);
	}
}

Java string reverse output

Please Enter String to Reverse =  string reverse
The result of String Reverse =  esrever gnirts

In this Java string reverse code, first, we converted the revStr string 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 StringReverse3 {
	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 String to Reverse =  ");
		revStr = sc.nextLine();
		
		char[] revCharArr = revStr.toCharArray();
		
		System.out.print("\nThe result of String Reverse =  " );
		for(i = revCharArr.length - 1; i >= 0; i--) {
			revCharArr[j++] = revStr.charAt(i);
		}		
		System.out.print(revCharArr);
	}
}
Please Enter String to Reverse =  Hello

The result of String Reverse =  olleH

Java Program to Reverse a String using While Loop

import java.util.Scanner;

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

		sc= new Scanner(System.in);

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

The first result of String Reverse  =  dlroW gnimmargorP avaJ
The Second result of String Reverse =  dlroW gnimmargorP avaJ

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 StringReverse5 {
	private static Scanner sc;
	public static void main(String[] args) {
		String revStr;
		char temp;

		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Reverse =  ");
		revStr = sc.nextLine();
		
		char[] revCharArr = revStr.toCharArray();
		int len = revStr.length() - 1;
		
		System.out.print("\nThe result of String Reverse =  " );
		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 String to Reverse =  Welcome to Java world

The result of String Reverse =  dlrow avaJ ot emocleW

Java String Reverse using StringBuffer

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

import java.util.Scanner;

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

		sc= new Scanner(System.in);

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

Java String Reverse using Recursion

import java.util.Scanner;

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

		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Reverse =  ");
		revStr = sc.nextLine();
		sc.close();
		
		String reversed = reverseString(revStr);
		
		System.out.print("\nThe result of String Reverse =  " + reversed );		
	}
	public static String reverseString(String revStr) {
		if(revStr.isEmpty()) {
			return revStr;
		}
		return reverseString(revStr.substring(1)) + revStr.charAt(0);
	}
}
Please Enter String to Reverse =  Learn Java

The result of String Reverse =  avaJ nraeL

It is an extension to the above Java string reverse program.

import java.util.Scanner;

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

		sc= new Scanner(System.in);

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

The result of String Reverse =  lairotuT avaJ