String manipulation is an important learning point for everyone in their programming journey. In essence, strings can be understood as a sequence of characters. In the Java programming language, strings are independent objects that support different operations and modifications.
Check out our free courses to get an edge over the competition.
When it comes to string manipulation, one of the foremost roadblocks is learning how to reverse a string in Java. Broadly, there are five ways you can go about reversing your string in Java.
This article walks you through all the five methods to help you understand different approaches toward using Java program to reverse a string.
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
1. String Reversal Using CharAt Method
Check out the code snippet below to understand how we use the CharAt() method to reverse an input string. In terms of working, this approach works by first extracting all the characters from the input string. Our method is mainly used to return characters at specific indices in the given string. Then, the characters can be appended to the string in the reverse order to arrive at the desired reversed string.
Check out upGrad’s Advanced Certification in DevOps
Explore our Popular Software Engineering Courses
When you execute the above mentioned Java program, you get the following output:
|Enter the input string
The output string reversed is as follows: olleH
This is one of the easier and more direct approaches you can take to reverse a String in Java. Now, let’s move ahead and look at some more techniques to get the same result – i.e., reversed string.
Check out upGrad’s Advanced Certification in Cyber Security
2. StringBuilder/String Buffer Class to Reverse a String
These two classes come inbuilt with a reverse() method that can be easily used to reverse your string. The method is programmed in a manner to replace your character sequence with the reverse ordered characters. The code snippet below gives an overview of how you can reverse a string in Java using StringBuilder/StringBuffer class.
Executing the above piece of code will return the following result on your screen:
|Reversed string is: darGpu ot emocleW
Alternatively, you can also use the StringBuffer class and its reverse() method in the same fashion as above. Here is a piece of code that does exactly that:
When you execute the above-mentioned code, you will get the same output as earlier. In essence, both the StringBuilder as well as the StringBuffer classes come inbuilt with a reverse() function that can quickly reverse your input strings without having the need to hard code it.
In-Demand Software Development Skills
While it is true that both StringBuffer, as well as StringBuilder, use the same approach to arrive at the final reversed string, StringBuilder is generally preferred. The reason for that is the speed and synchronicity of the StringBuilder class as compared to the StringBuffer class.
Now, let’s move to other methods of reversing a string in Java!
3. Reverse Iteration to Reverse a String
This approach of reversing a string in Java takes a more programming-oriented approach and tries to reverse the string by using logical constructs. To do this, we first convert the input string into an array of different characters. This is done using the CharArray() method. Then, we just iterate over the given array in reverse order and keep printing the characters at each index. That way, we have our reversed string as the output! Here is how that is done:
On executing the above code, you get the following output. Both the input strings have been reversed as was required:
Read our Popular Articles related to Software Development
darGup ot emocleW
This approach is fairly straightforward. Once you have the array of characters from the input string, you can manipulate it whichever way you like. Remember, strings are immutable, but arrays are not. So that idea can be used to our advantage once we have an array of characters from the string, and wish to perform any string manipulations. Now, let’s look at another approach for string reversal in Java – one that uses the concept of recursion.
4. Recursion for reversing the string
Recursion is a fancy way of saying “a function that calls itself”. However, it is an extremely important concept in the world of computer science and forms the base of many, more complex, concepts and theories.
The following example shows how recursion can help us arrive at our reverse string in Java.
On running the above code, you get the following output:
|Enter the string : upGrad is for one and all! Java is the blooming technology since its existence
Reversed String: !lla dna eno rof si darGpu”
If you’re having trouble understanding the above program, don’t worry. We have simply created the object for our StringRecursion class. Using the sc.nextLine() method, we read the input string character by character and store it in the variable s. Then, we call the reverse method to get the final work done and arrive at the desired output.
5. Reversing different letters in a string
While this is not directly related to string reversal, it is a good approach, and you should know this. While we don’t want to reverse the entire string, we want to reverse each word in the string. So, “Hello people” will not become “elpoep olleH”, but “olleH elpoep”.
Here is how this can be achieved using Java:
When you execute the above code, you will get the following output:
With that, we come to the end of the different ways of performing string reversal in Java. Our advice is that you take the hands-on to learning. Now that you know the basics of how to reverse string in Java, you should open a compiler and start programming. Play around with the different functions, try out different input strings, and see what you find!
Learning by experimenting is undoubtedly the best way to gain knowledge, especially when it is about learning programming or other problem-solving techniques.
At upGrad, we understand the importance of learning by doing, which reflects in all of our courses and training initiatives. Our practical curriculum ensures you get all the required theoretical and hands-on training.
Is there any inbuilt reverse function in Java?
Yes, the reverse function in Java has an inbuilt method with the StringBuilder and StringBuffer classes.
Can we do string reversal in Java without using inbuilt functions?
Sure, you can efficiently perform string reversal in Java using iteration, recursion, looping, and other programming constructs without requiring inbuilt classes. You must first convert your string into a character array, and then things will become easier.
Is string reversal in Java difficult?
No, Java provides multiple ways to perform string reversal, and all of those ways are pretty intuitive and easy to understand. Just remember - you need to do it to know it, just reading won’t cut it!