how to reverse string in java

reverse a string in java

You might have come across a case where you need to reverse the characters in a string or an array. Though it might sound easy, but it is not as straightforward as many of you would think.

To reverse a string means to change its character sequence from start to finish (from left to right) or vice versa. It is a technique used commonly in puzzles and games, where the players are given clues that are all worded backwards.

reverse a string in java

It is not exactly rocket science; there are algorithms available for this purpose. Many programming languages have built-in functions that can do this job.

If your language doesn’t support any such native method, you can build your own functions with ease. This article explains different ways of reversing strings in Java, with examples and illustrations so that you can understand them better:

Reverse String using Java’s built-in function – reverse()

All modern programming languages have built-in functions to reverse the order of string characters. In Java, the String class has a reverse() method that can reverse a String in-place.

This function is a static method that does not require an object to be used. It takes a String as argument and returns a new String with the characters in reverse order.

To reverse a String in Java, write code as shown below. You can also use this code to reverse any String containing characters of any language. The language of the String does not matter.

Let’s understand this with the help of an example.

Assume we have a String called myString that reads as “My String”. Now we want to reverse it and get the final String as “ngir Sram”. Let’s write code to achieve this: Here, myString is the String that we want to reverse. Let’s break down the above code:

Reverse String in Java with a loop – how to reverse a string in java

You can also reverse a String in Java by using a for loop. This is an alternate method of reverse string in Java. The syntax for this is:

Here, we have declared an ArrayList of String, reversedArrayList, to hold the reversed string. For this, we have used a while loop to iterate from the last character to the first character. Then, we get certain characters from the original String and add them to the newly created reversed string using the += operator.

Let’s understand this with the help of an example. Assume we have a String called myString that reads as “My String”. Now we want to reverse it and get the final String as “ngir Sram”. Let’s write code to achieve this: Here, myString is the String that we want to reverse. Let’s break down the above code:

Reversing words in a String using split() and join() methods

While reversing characters in a String works fine in most cases, it does not work for words. Let’s assume you have a String called myString that reads as “This is a String”. Now you want to reverse this and get the final String as “String a is This”. Let’s write code to achieve this:

Here, we have split the myString into words using the split() method. This method splits the String at each occurrence of the specified separator. We have passed an empty space “” as the separator. Split() returns a java.util.List object containing the individual words and we have stored it in a String array named wordsArray. We have then joined all the words together, with a space between each word, and written it to the console.

Reversing characters of a String using ASCII value

If you want to reverse characters of a String, you can do so by taking the ASCII value of each character and then assigning it to its reverse character.

Let’s assume you have a String called myString that reads as “My String”. Now you want to reverse it and get the final String as “ngir Sram”. Let’s write code to achieve this:

Here, we have used an if statement to check whether the ASCII value of each character is less than the ASCII value of its corresponding reverse character. In case the condition is false, we have assigned 0 to that character.

In this method, we have to remember the ASCII values of the characters for both their forward and backward sequence. Let’s break down the above code:

Reversing String using Complementary Characters

You can also reverse a String in java by using Complementary Characters. Let’s assume you have a String called myString that reads as “My String”. Now you want to reverse it and get the final String as “ngir Sram”. Let’s write code to achieve this:

Here, we have assigned the Complementary Character of each character in the original String to its corresponding character in the final String. Let’s break down the above code:

Conclusion

Reverse a String in Java is a tricky task that requires a bit of effort. You can use built-in functions, or write a loop or even use Complementary Characters to reverse a String in Java. Select any one of them as per your requirements.

If you have any questions or suggestions, please drop a comment below. We would love to hear from you.

## Reverse String in Java with a loop

You can also reverse a String in Java by using a for loop. This is an alternate method of reversing a String in Java. The syntax for this is: Here, we have declared an ArrayList of String, reversedArrayList, to hold the reversed string.

For this, we have used a while loop to iterate from the last character to the first character. Then, we get certain characters from the original String and add them to the newly created reversed string using the += operator.

Let’s understand this with the help of an example. Assume we have a String called myString that reads as “My String”. Now we want to reverse it and get the final String as “ngir Sram”. Let’s write code to achieve this:

Here, myString is the String that we want to reverse. Let’s break down the above code:

Reversing words in a String using split() and join() methods

If you want to reverse words in a String, you can do so by using the split() and join() methods. Let’s assume you have a String called myString that reads as “This is a String”.

Now you want to reverse this and get the final String as “String a is This”. Let’s write code to achieve this: Here, we have split the myString into words using the split() method.

This method splits the String at each occurrence of the specified separator. We have passed an empty space “” as the separator.

Split() returns a java.util.List object containing the individual words and we have stored it in a String array named wordsArray. We have then joined all the words together, with a space between each word, and written it to the console.

Reversing characters of a String using ASCII value

If you want to reverse characters of a String, you can use the ASCII value of each character to find the corresponding character in its reverse sequence.

Let’s assume you have a String called myString that reads as “My String”. Now you want to reverse it and get the final String as “ngir Sram”.

Let’s write code to achieve this: Here, we have used an if statement to check whether the ASCII value of each character is less than the ASCII value of its corresponding reverse character.

In case the condition is false, we have assigned 0 to that character. In this method, you have to remember the ASCII values of the characters for both their forward and backward sequence.

Add a Comment

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