String reverse in Java
In this post, you will learn how to reverse a string using the Java programming language. There are different ways to reverse a string in Java. Here, we have mentioned most of them.
Java reverse a string using CharAt method
Java provides the CharAt() method that returns the character at the specified index in the given string. Here, we have used the CharAt() method in order to extract the characters from the input string and then, we have appended them in reverse order to reverse the given string.
import java.io.*;
import java.util.Scanner;
public class RevString {
public static void main (String[] args) {
String str= "etutorialspoint", nstr="";
char ch;
System.out.println("Original word: "+ str);
for (int i=0; i<str.length(); i++)
{
//extracts each character
ch= str.charAt(i);
//adds each character in front of the existing string
nstr= ch+nstr;
}
System.out.println("Reversed word: "+ nstr);
}
}
Output of the above code:
Original word: etutorialspoint
Reversed word: tniopslairotute
Java reverse a string using ByteArray
The Java String class getBytes() method performs the encoding of a string into a sequence of bytes and keeps it in an array of bytes. In the given example, first we create a temporary byte[] of length equal to the length of the string. After that, we iterate the given array in the reverse order.
// Java program to Reverse
// String using ByteArray.
import java.lang.*;
import java.io.*;
import java.util.*;
public class ReverseString {
public static void main(String[] args)
{
String str = "etutorialspoint";
System.out.println("Original String: "+str);
// Converting string into bytes[].
byte[] byteArr = str.getBytes();
byte[] result = new byte[byteArr.length];
// Store result in reverse order into the
// result byte[]
for (int i = 0; i < byteArr.length; i++)
result[i] = byteArr[byteArr.length - i - 1];
System.out.println("Reversed String: "+new String(result));
}
}
Output of the above code:
Original String: etutorialspoint
Reversed String: tniopslairotute
Java reverse a string using StringBuilder class
The StringBuilder in Java represents a mutable sequence of characters. The reverse() is an in-built method of the class StringBuilder which is used to reverse the characters in the StringBuffer. It replaces the character sequence in the reverse order. The given example demonstrates how to reverse a string using the reverse() method of the StringBuffer class.
// Java program to Reverse
// String using StringBuilder
import java.util.*;
public class ReverseString{
// function to reverse a string in Java
// using StringBuilder
public static String rev(String s){
return new StringBuilder(s).reverse().toString();
}
public static void main(String[] args){
String str= "Welcome to etutorialspoint";
System.out.println("Original string: "+str);
str = rev(str);
System.out.println("Reversed string: "+str);
}
}
Output of the above code:
Original string: Welcome to etutorialspoint
Reversed string: tniopslairotute ot emocleW
Java reverse a string using Reverse Iteration
In the given example, we have used the CharArray() method to convert the given string to character array. After that, we have iterated the given array in the reverse order.
// Java program to Reverse
// String using Reverse Iteration
import java.util.*;
public class StringRev{
// Function to reverse a string in Java
public static String reverseString(String str){
// converting the string into a character array
char c[]=str.toCharArray();
String reverse="";
// for loop to reverse a string
for(int i=c.length-1;i>=0;i--){
reverse+=c[i];
}
return reverse;
}
public static void main(String[] args) {
String str= "Welcome to etutorialspoint";
System.out.println("Original string: "+str);
str = reverseString(str);
System.out.println("Reversed string: "+str);
}
}
Output of the above code:
Original string: Welcome to etutorialspoint
Reversed string: tniopslairotute ot emocleW
Java reverse a string using Recursion
Recursion function is a function which is called by itself. In the given example, we call the recursion function to reverse a given string. A recursion function continues until some condition is met to prevent it. That's why we use the if-else statement to break the infinite recursion.
// Java program to Reverse
// string using Recursion
import java.util.*;
public class ReverseStringRecursion{
String rev(String str) {
if(str.length() == 0)
return " ";
return str.charAt(str.length()-1) + rev(str.substring(0,str.length()-1));
}
public static void main(String[ ] args) {
ReverseStringRecursion strre=new ReverseStringRecursion();
String str= "Welcome to etutorialspoint!";
System.out.println("Original string: "+str);
System.out.println("Reversed String: "+strre.rev(str));
}
}
Output of the above code:
Original string: Welcome to etutorialspoint!
Reversed String: !tniopslairotute ot emocleW
Related Articles
Sort array in ascending order Java
Automorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java