How to Reverse A String in Java Letter by Letter?

A typical programming task that can be accomplished in a number of ways is reversing a string. Reversing a string letter by letter is one of the most straightforward techniques. We will go through letter-by-letter string reversing in Java in this tutorial.

Let's first grasp the fundamentals of a string in Java before moving on to the implementation. In Java, a string is a collection of characters. Strings are immutable in Java, therefore once an object of a string is generated, it cannot be changed. As a result, in order to reverse a string, a new string object must be created with the characters arranged in reverse.

Using a loop to cycle through the string and append each character to a new string in reverse order is the simplest approach to reverse a string in Java, letter by letter. Here is the key:

ReverseString.java

Output:

Original String: Hello World!
Reversed String: !dlroW olleH

Let's analyse the code. The initial string is first defined as "Hello World!" The reversed string will then be stored in an empty string created later with the name reversedString.

After that, we traverse through the initial string backwards using a for loop, beginning at the final character (length-1) and terminating at the first character. (0). To append each character from the original string to the inverted string inside the loop, we use the concatenation operator (+) and the charAt() method to retrieve the character from the original string.

The println() function is then used to print the original and reversed strings.

As you can see, the original string was successfully inverted letter by letter by the code.

It is important to keep in mind that this method makes a new string object in memory for each character that is added, which can be ineffective for long strings. In reality, concatenating individual strings to create the reversed string is frequently preferable to using a StringBuilder or StringBuffer.

Here are two additional methods for letter-by-letter string reversal in Java:

Logic 2: Using a character array

Reversing a string in Java can also be accomplished by breaking it into a character array, iterating through the array, and swapping the characters at the ends of the array until they meet in the middle. This is the code:

ReverseString.java

Output:

Original String: Hello World!
Reversed String: !dlroW olleH

Let's dissect the code. First, we use the toCharArray() method to transform the original string into a character array. After that, we initialise two pointers, left and right, to the array's first and last characters, respectively.

The characters at the left and right places are then switched until the left pointer crosses the right pointer using a while loop to run through the array. To accomplish this, we first store the leftmost character in a temporary variable before switching it out for the rightmost character. The character from the temporary variable is then used to replace the character that is currently in the proper spot.

To complete the iteration, we finally shift the left pointer to the right and the right pointer to the left.

When the loop is finished, we use the String constructor, which accepts a char array as a parameter, to turn the character array back into a string.

Logic 3: Using recursion

Recursion is a third technique for reversing a string in Java letter by letter. A recursive function that reverses a substring of the original string as input and then calls itself with the substring omitting the initial character is possible to create. Here is the key:

ReverseString.java

Output:

Original String: Hello World!
Reversed String: !dlroW olleH

Take the code for example. We begin by defining the first string as "Hello World!" The original string is then passed to the reverse() function, which returns the reversedString string as its output.

With a string as its input and a reversed string as its output, the reverse() method is recursive. We just return the input text in its original form if the input string's length is less than or equal to 1.

If not, the input string's substring, beginning with the second character (s.substring(1)), is used to repeatedly invoke the reverse() method. The concatenation operator (+) is then used to add the input string's beginning character (s.charAt(0)) to the end of the substring that has been reversed.

The reversed string is built up from the bottom of the call stack and given back to the caller after the recursion continues until the input string has been reduced to a single character.

Although recursion is an elegant way to reverse a string, because it generates a new string object with each recursive call, it is sometimes less effective than the other approaches.

There are several approaches of letter-by-letter reversing a string in Java, each having pros and cons. Performance, readability, and personal choice are just a few examples of the variables that may influence the strategy you choose.

The task of letter-by-letter reversing a string in Java is an easy one that can be completed with the help of a plain loop. Strings of any length or complexity can be reversed with ease if you know the fundamentals of Java strings and the necessary data structures.