How To Reverse A String In Java (5 ways)
In this article, we will learn how to reverse strings in Java. I have write down 5 diffrent ways to reverse strings. Have a look!
Introduction
This question is simple but important since it is asked in most of the java interviews. You will be asked to reverse a string using a ready to use library method (like reverse()
of StringBuilder
) or without using a library method. In this article, we will see both ways.
Below I have listed the 5 ways to reverse a string in Java. In all 5 cases, I will explain the step by step logic and gave the Java program for the same.
-
Using charAt() method of String
-
Using getBytes() method of String
-
Using toCharArray() method of String
-
Using reverse() method of StringBuilder
-
Using reverse() method of Collections
1. Using charAt()
method of String
charAt()
method of the Java String returns character value at the specified index of the string.
1
2
3
4
5
6
7
8
9
public char charAt(int index)
where 'index' is the index of the character specified
returns - char at specified index
ex.
String blogName = "codeRolls.com";
char returnedChar = blogName.charAt(3); // returns e
I hope you have understood how the charAt()
works, now we will see how can we use the same charAt()
method to reverse the string.
-
Declare a string that you have to reverse. eg.
blogName
-
Initialize an empty string with the name
reversedString
. -
Apply
for
loop to get the characters of theblogName
in the reverse order i.eint i = blogName.length()-1; i <= 0; i- -;
-
Inside the for loop append each character with the
reversedString
. -
Print the
reversedString
.
A Java program to reverse the string using charAt()
method of the String class is given below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* A Java program to reverse a string.
* We are using 'charAt()' method of the String class to get all char and arrange it in
* descending order to get a reverse string.
*
* @author Gaurav Kukade at coderolls.com
*/
public class ReverseString {
public static void main(String[] args) {
String blogName = "coderolls.com";
String reversedString = "";
for(int i = blogName.length()-1; i>=0; i--){
reversedString = reversedString + blogName.charAt(i);
}
System.out.print("The reversed string of the '"+blogName+"' is: " );
System.out.println(reversedString);
}
}
Output:
1
The reversed string of the 'coderolls.com' is: moc.slloredoc
Note:
In the for
loop, I have assigned blogName.length()-1
to i instead of blogName.length()
because the characters in the string are indexed from 0.
I have given an image below to show the string indexing.
Also you can watch the video on my youtube channel for reverse a string with the above method.
2. Using getBytes()
method of String
In the getBytes()
method of Java String first encodes the specified string into the sequence of bytes using the platforms default charset and then save the result in the byte array. The same byte array will be returned.
1
2
3
4
5
6
7
8
9
public byte[] getBytes()
It encodes the string into sequence of the bytes and return the byte array with this result
returns - byte array of the encoded sequence of bytes
ex.
String blogName = "coderolls.com";
byte [] stringByteArray = blogName.getBytes();
Now we will see the step by step logic to reverse the string using the getBytes()
method
-
Declare a string that you have to reverse. eg.
blogName
-
Create a
byte []
to store the byte array of the specified string. Let saystringByteArray
-
Create another
byte []
with the same length of the previousbyte [] stringByteArray
. Let sayreversedStringByteArray
-
Create a string
reversedString
. -
Apply for loop to iterate over
stringByteArray
. -
Using for loop set all the elements of the
stringByteArray
in reverse order withreversedStringByteArray
. -
Create a new String object using
reversedStringByteArray
and assign it to thereversedString
. -
Print
reversedString
.
The program to reverse the string using getBytes()
method of the Java String is given below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* A Java program to reverse a string.
*
* We are using 'getByets()' method of the String class to get byte array of the specified string
* and assign it to the another byte array of the same length in the descending order.
*
* New String object of the reversed byte array will give reversed string.
*
* @author Gaurav Kukade at coderolls.com
*/
public class ReverseStringUsingGetBytes {
public static void main(String[] args) {
String blogName = "coderolls.com";
byte[] stringByteArray = blogName.getBytes();
byte[] reverseStringByteArray = new byte[stringByteArray.length] ;
String reversedString;
for(int i = 0; i<=stringByteArray.length-1; i++) {
reverseStringByteArray [i] = stringByteArray[stringByteArray.length-i-1];
}
reversedString = new String(reverseStringByteArray);
System.out.print("The reversed string of the '"+blogName+"' is: " );
System.out.println(reversedString);
}
}
Output:
1
2
The reversed string of the 'coderolls.com' is: moc.slloredoc
Note: Like character indexing of the string, elements of the array starts from 0; that’s why I have used stringByteArray.length-1 in the for loop.
3. Using toCharArray() method of String
toCharArray()
is an instance method of the String class in Java.
toCharArray()
method returns a new character array of the string specified.
1
2
3
4
5
6
public char [] toCharArray()
This method converts the string into new character array of the same length containing the character sequence represented by the string.
returns - new char array containing character sequence represented by the string.
Now we will see how can we use the toCharArray() method to reverse the string.
Below I have given the step by step logic for the program to reverse string using toCharArray()
method.
-
Declare a string that you have to reverse. eg.
blogName
-
Create a new char array using the
blogName
string. eg.stringCharArray
-
Create an empty string, we will be using that string to append with elements of the
stringCharArray
. Let say the empty string isreversedString
. -
Apply for loop to iterate
stringCharArray
in the reverse order. -
In the for loop, append the
reversedString
with thei
th element of thestringCharArray
. -
Print the
reversedString
.
I have given the program as per the above logic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* A Java program to reverse a string.
*
* We are using 'toCharArray' method of the String class to get char array of the specified string
* and append it with the temp string in reverse order.
*
* @author Gaurav Kukade at coderolls.com
*/
public class ReverseStringUsingToCharArray {
public static void main(String[] args) {
String blogName = "coderolls.com";
char [] stringCharArray = blogName.toCharArray();
String reversedString = "";
for(int i = stringCharArray.length-1; i>=0; i--) {
reversedString = reversedString + stringCharArray[i];
}
System.out.print("The reversed string of the '"+blogName+"' is: " );
System.out.println(reversedString);
}
}
Output:
1
The reversed string of the 'coderolls.com' is: moc.slloredoc
4. Using reverse() method of StringBuilder
reverse()
method of the StringBuilder
class replaces the character sequence represented by a string in the reverse order.
1
2
3
4
5
public StringBuilder reverse()
reverse() method reverse the char sequence of the string
return - reference to the StringBuilder object with reverse char sequence
I have given the step by step logic for the program to reverse the string using reverse()
method of StringBuilder
class.
-
Declare a string that you have to reverse. eg.
blogName
-
Create a new instance of the
StringBuilder
class as astringBuilder
usingblogName
string. -
Apply
reverse()
on thestringBuilder
instance and assign it to the samestringBuilder
instance. -
Print the
stringBuilder
instance.
I have written the Java program to reverse the string using reverse()
method of the StringBuilder
class below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* A Java program to reverse a string.
*
* We are using 'reverse()' method of the StringBuilder class. This method rverse the
* character sequence represented by the string.
*
* @author Gaurav Kukade at coderolls.com
*/
public class ReverseStringUsingReverseMethodOfStringBuilder {
public static void main(String[] args) {
String blogName = "coderolls.com";
StringBuilder stringBuilder = new StringBuilder(blogName);
String reversedString = stringBuilder.reverse().toString();
System.out.print("The reversed string of the '"+blogName+"' is: ");
System.out.println(reversedString);
}
}
Output:
1
The reversed string of the 'coderolls.com' is: moc.slloredoc
5. Using reverse() method of Collections
reverse()
method of the Collections take list as a parameter and reverse the order of elements.
1
2
3
4
5
6
public static void reverse(List<?> list)
this method reverse the order of elements in the specified list
parameters
list - list whose elements you have to reverse
The logic for the program to reverse a string using the reverse()
method of the Collections is given below.
-
Declare a string that you have to reverse. eg.
blogName
-
Create a new char array using the
blogName
string. eg.stringCharArray
-
Create a new
ArrayList
. eg.arrayList
-
Create an empty string, we will be using that string to append with elements of the
arrayList
. Let say the empty string isreversedString
. -
Apply
forEach
loop to iterate over thestringCharArray
. In theforEach
loop add each element of thestringCharArray
toarrayList
-
Use
reverse()
method of the Collections to reverse the order of elements inarrayList
. -
Apply
forEach
loop to iterate overarrayList
. In theforEach
loop append thereversedString
with each element of thearrayList
. -
Print
reversedString
.
The program for the above logic is given below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* A Java program to reverse a string.
*
* We are using 'reverse()' method of the Collections. This method reverse the
* order of elements of the list specified.
*
* @author Gaurav Kukade at coderolls.com
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ReverseStringUsingReeverseMethodOfCollections {
public static void main(String[] args) {
String blogName = "coderolls.com";
char [] stringCharArray = blogName.toCharArray();
List<Character> arrayList = new ArrayList<>();
String reversedString = "";
for(char ch : stringCharArray) {
arrayList.add(ch);
}
Collections.reverse(arrayList);
for( Character ch: arrayList) {
reversedString = reversedString + ch;
}
System.out.print("The reversed string of the '"+blogName+"' is: ");
System.out.println(reversedString);
}
}
Output:
1
The reversed string of the 'coderolls.com' is: moc.slloredoc
So we have seen all five ways to reverse the string in Java.
Conclusion
We can reverse the string in multiple ways. I have listed the five ways below.
-
Using
charAt()
method of StringIn the first way, we have used
charAt()
method to get every character of the specified string and append it to an empty string in the reveres order. -
Using
getBytes()
method of StringIn a second way, I have converted a string into byte Array and created another byte array with same length. Then I have inserted elements of byte array in the new byte array in the reverse order. Finally, convert new byte array to string to get the reverse string.
-
Using
toCharArray()
method of StringIn a third way, I have converted the string to the character array using the
toCharArray()
method. Then I have to append and an empty string with every character of the character array in the reverse order. -
Using reverse() method of
StringBuilder
In a fourth way, I have created a
stringBuilder
instance using the string, which I have to reverse. And finally, I have used thereverse()
method of theStringBuilder
class to get the reversed string. -
Using reverse() method of Collections
In the last way, I have created an
ArrayList
using the string, which I have to reverse. Then I have usedreverse()
method of the Collections to reverse the order of elements in theArrayList
. And finally, I have appended an empty string with every element of theArrayList
to get the reversed string.
These are the ways we can reverse the string in Java.
If you have any queries or any suggestions or if you find any mistakes in the article or in the code snippet given above please feel free to comment below.
Have you try the different ways to reverse the string in the java? or do you know the most simple way? please write it down in the comment section below.
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!