Reverse a String using character array in Java
This post will discuss how to reverse a string using a character array in Java.
We know that we cannot make any change in the string object as string is immutable in Java. But we can use a character array that can be modified easily:
- Create an empty character array of the same size as that of the given string.
- Fill the character array backward with characters of the given string.
- Finally, convert the character array into string using
String.copyValueOf(char[])and return it.
The following program demonstrates it:
|
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 35 |
class Main { // Method to reverse a string in Java using a character array public static String reverse(String str) { // return if the string is null or empty if (str == null || str.equals("")) { return str; } // get string length int n = str.length(); // create a character array of the same size as that of string char[] temp = new char[n]; // fill character array backward with characters in the string for (int i = 0; i < n; i++) { temp[n - i - 1] = str.charAt(i); } // convert character array to string and return it return String.copyValueOf(temp); } public static void main(String[] args) { String str = "Reverse me!"; // String is immutable str = reverse(str); System.out.println("The reversed string is " + str); } } |
Output:
The reversed string is !em esreveR
Using swap():
Following is another efficient way to reverse a string in Java using character array:
- Create a character array and initialize it with characters of the given string using
String.toCharArray(). - Start from the two endpoints
landhof the given string. Run the loop till two endpoints intersect(l <= h). In each iteration of the loop, swap values present at indexes l & h and increment l & decrement h. - Finally, convert the character array into string using
String.copyValueOf(char[])and return.
The following program demonstrates it:
|
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 35 |
class Main { // Method to reverse a string in Java using a character array public static String reverse(String str) { // return if the string is null or empty if (str == null || str.equals("")) { return str; } // create a character array and initialize it with the given string char[] c = str.toCharArray(); for (int l = 0, h = str.length() - 1; l < h; l++, h--) { // swap values at `l` and `h` char temp = c[l]; c[l] = c[h]; c[h] = temp; } // convert character array to string and return return String.copyValueOf(c); } public static void main(String[] args) { String str = "Reverse me!"; // String is immutable str = reverse(str); System.out.println("The reversed string is " + str); } } |
Output:
The reversed string is !em esreveR
That’s all about reversing a String using a character array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)