All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Reverse Of A String In Java

Last Updated : Mar 11, 2024

Reverse Of A String In Java

In this article we will show you the solution of reverse of a string in java, reversing a string means changing its order, so that the last character becomes the first, the second last becomes the second, and so on.

We will explore different approaches to solve this problem and provide step-by-step explanations for each approach.

To reverse a string, we can use various methods in Java.

One common approach is to convert the string to a character array, swap the characters from the beginning and end of the array, and then convert the modified array back to a string.

Another approach is to use the StringBuilder or StringBuffer class, which provide convenient methods for reversing a string. We will discuss both methods in this tutorial.

Step By Step Guide On Reverse Of A String In Java :-

public static String reverseStringCharArray(String str) {
    char[] charArray = str.toCharArray();
    int left = 0;
    int right = charArray.length - 1;
    while (left < right) {
        char temp = charArray[left];
        charArray[left] = charArray[right];
        charArray[right] = temp;
    return new String(charArray);
  1. We define a method called reverseStringCharArray that takes a string (str) as input and returns a reversed string.
  2. We convert the input string (str) into a character array using the toCharArray() method.
  3. We initialize two pointers, left and right, pointing to the first and last index of the character array, respectively.
  4. We swap the characters at the left and right positions using a temporary variable.
  5. We increment left and decrement right to move towards the center of the array.
  6. We repeat steps 4 and 5 until left becomes greater than or equal to right.
  7. Finally, we create a new string from the modified character array using the String constructor and return the reversed string.

Conclusion :-

In this tutorial, we explored two methods for reversing a string in Java.

We learned how to reverse a string using a character array and the StringBuilder class.

Both approaches provide efficient ways to reverse a string, and the choice between them depends on the specific requirements of your program.

Remember to choose the method that best suits your needs and coding style.

Reversing a string is a common operation in many programming scenarios, and understanding these techniques will be beneficial for your Java programming skills.

I hope this article on reverse of a string in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪