Reverse string in java

Reverse string in java means that swap first character with last and second character with second last and so on . In this post , we will discuss different methods for write java program to reverse a string as following .

  1. Reverse a string in java using for loop
  2. Reverse a string in java using While loop
  3. Reverse a string in java using Recursion
  4. Reverse a string in java using array
  5. Reverse a string in java using String Buffer

Reverse a String in java using for loop

In this method , we see how to reverse a string in java using for loop . We will follow these steps for reverse a string in java using for loop .

  1. Declare  a string str .
  2. Take a empty string reverse , which is our final output as reverse string.
  3. Iterate str in reverse order till i >= 0 and each iterate append char at the end of reverse string with the help of charAt() method.
public class ReverseStringUsingForloop {

	public static void main(String[] args) {
		String str = "javavogue";
		String reverse = "";
		for (int i = str.length()-1; i >= 0 ; i--) {
			reverse = reverse + str.charAt(i);
		}
		
		System.out.println("reverse string is :"+ reverse);
	}
}

Output :

reverse string is :eugovavaj

We have reverse a string in java using for loop. we can reverse a sentence in java or reverse a word in java using same approach . Now we will write a java program to reverse a string using while loop .

Reverse a string in java using While loop

In this method we will use while loop instead of for loop in above approach . In this example we will reverse a string in java without using inbuilt function .

public class ReverseStringUsingWhile {

	public static void main(String[] args) {
		String str = "javavogue";
		String reverse = "";
		int i = str.length()-1; 
		while(i >= 0 ) {
			reverse = reverse + str.charAt(i);
			i--;
		}
		System.out.println("reverse string is :"+ reverse);
	}
	
}

Output :

reverse string is :eugovavaj

We have seen java code to reverse a string using while loop .we can reverse sentence in java without using split using this example also . Now we will see java how to reverse a string using recursion .

Reverse a string in java using Recursion

We will write a program for reverse a string in java using recursion. we use base case till string length is = 1

public class ReverseStringUsingRecursion {
	public static void main(String[] args) {
		String str = "javavogue";
		System.out.println("reverse string is =" + recursiveReverse(str));
	}

	public static String recursiveReverse(String str) {
		if (str.length() == 1)
			return str;
		else
	return str.charAt(str.length() - 1)
		+ recursiveReverse(str.substring(0, str.length() - 1));
	}
}

Output :

reverse string is =eugovavaj

In this example we have used substring() method of string for reverse a string .

Reverse a string in java using array

In this method we will create array from string using toArray() method of string . Now We Iterate this char array in reverse order and each iteration append char from char array to reverse string. we can reverse char array java using same approach .

public class ReverseStringUsingArray {
	
	public static void main(String[] args) {
		String str = "javavogue";
		String reverse = "";
		char arr [] = str.toCharArray();
		int i = arr.length -1;
		while(i >= 0 ) {
			reverse = reverse + arr[i] ;
			i--;
		}
		System.out.println("reverse string is :"+ reverse);
	}
}

Output :

reverse string is =eugovavaj

Reverse a string in java using String Buffer

In this example we see how to reverse string in java using string buffer’s reverse method .

public class ReverseStringUsingStringBuffer {
	
	public static void main(String[] args) {
		String str = "javavogue";
		StringBuffer buffer = new StringBuffer(str);
		System.out.println("reverse string is ="+ buffer.reverse());
	}
}

Output :

reverse string is =eugovavaj

You can see top coding interview question and top string program for interview .

Leave a Reply

Your email address will not be published. Required fields are marked *

8 + 1 =