DEV Community

Sangeethraj
Sangeethraj

Posted on

Reverse a String in Java

Java Program to reverse a string using StringBuilder

Explanation :
Strings are Objects in Java that are internally supported by a char array. Strings are immutable because arrays are immutable(closed for modification). Every time you make a change to a String, a new String is produced. So that we are using StringBuilder which is mutable. Note: we can use StringBuffer class as well.

Code :

public class ReverseStringBuilder {

    public static void main(String[] args) {

        /* String is immutable */
        String name = "Palindrome";

        /* Create StringBuilder(mutable) object */
        StringBuilder s1 = new StringBuilder();

        /* Using append() and reverse() in StringBuilder */
        s1.append(name);
        s1 = s1.reverse();

        /* Print the reverse */
        System.out.println(s1);

    }

}
Enter fullscreen mode Exit fullscreen mode

Output :
emordnilaP

Steps :

  1. Create a String class object and initialize it.
  2. Create a object of string builder class.
  3. Use the inbuilt function of string builder append() and reverse().
  4. Finally print string builder object.

Java Program to reverse a string without using String inbuilt function reverse().

Method 01
Explanation :
The toCharArray() function is used to transform that string into a character array. Following that, we'll use the for loop to run through each character in reverse order, get output for each character.

Code :

public class Reverse {

    public static void main(String[] args) {

        /* String is immutable */
        String name = "Palindrome";

        /* Using toCharArray() function */
        char[] ch = name.toCharArray();

        /* Temp string */
        String rev = "";

        /* Iterating for loop in reverse to store */
        for (int i = ch.length - 1; i >= 0; i--) {
            /* Concatenating Strings */
            rev += ch[i];
        }

        /* Print the reverse */
        System.out.println(rev);

    }

}
Enter fullscreen mode Exit fullscreen mode

Output :
emordnilaP

Steps :

  1. Create a String class object and initialize it.
  2. Create a char array and call the toCharArray() function with String object.
  3. Creating a String class object for temp variable.
  4. Iterating the for loop in reverse to get each character in reverse order.
  5. Concatenating each character in Temp variable.
  6. Finally print Temp.

Method 02
Explanation :
Using the for loop, we printed the string in reverse order. The charAt(index) method, on the other hand, returns the character at any given index. The character will be concatenated after each iteration to reverse the string variable.

Code :

public class ReverseCharAt {

    public static void main(String[] args) {

        /* String is immutable */
        String name = "Palindrome";

        /* Temp string */
        String rev = "";

        /* Iterating for loop in reverse to store */
        for (int i = name.length() - 1; i >= 0; i--) {
            /* Concatenating Strings */
            rev = rev + name.charAt(i);
        }

        /* Print the reverse */
        System.out.println(rev);

    }

}
Enter fullscreen mode Exit fullscreen mode

Output :
emordnilaP

Steps :

  1. Creating a String class object and initialize it.
  2. Creating a String class object for temp variable.
  3. Iterating the for loop in reverse to get each character in reverse order.
  4. Concatenating each character to Temp variable by calling charAt() function.
  5. Finally print Temp.

Special Note: The reverse function is familiar to you but the motive is to introduce the possibility of StringBuilder and how you can optimize code without a reverse function. Hope this will help someone in future!! I welcome your feedback and solutions-oriented suggestions, thank you for your time

Happy Coding❤️❤️❤️

Top comments (0)