How to reverse a string in Java

How to reverse a string in Java:

String is immutable in Java. It means, we can’t modify a string object. If we want any change to a string, we need to create one different string object. String class also don’t have any method to reverse a string.

In this post, we will learn different ways to reverse a string in Java using different ways.

Method 1: Iterating through the characters of the string:

We can iterate throught the characters of the string in a reverse order and append them to build the reverse string. Java String provides a method called charAt, that can be used to get one character at any specific position by using the index.

Below is the complete program:

import java.util.Scanner;

class Example {
    public static void main(String[] args) {
        String originalStr;
        StringBuilder reverseStrBuilder;
        String reverseStr;

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a string to reverse :");
        originalStr = scanner.nextLine();

        reverseStrBuilder = new StringBuilder();

        for (int i = originalStr.length() - 1; i >= 0; i--) {
            reverseStrBuilder.append(originalStr.charAt(i));
        }

        reverseStr = reverseStrBuilder.toString();

        System.out.println("Reverse string : " + reverseStr);
    }
}

Here,

  • originalStr variable is used to store the original string.
  • reverseStrBuilder is a StringBuilder used to build the final string.
  • The for loop iterates through the characters of the string originalStr one by one from the last character to the start character. It appends all characters to the string builder originalStrBuilder.
  • Once the for loop ends, it is converting the string builder reverseStrBuilder to a string.

If you run this program, it will give one output as like below:

Enter a string to reverse :
hello world
Reverse string : dlrow olleh

Method 2: By using StringBuffer class:

We can also reverse a string by using a StringBuffer class. We can create one StringBuffer by passing the string to the constructor of it. It returns another StringBuffer. We can use the reverse() method on the StringBuffer object to reverse the contents. Finally, we can use toString to join and change it to a string.

import java.util.Scanner;

class Example {
    public static void main(String[] args) {
        String originalStr;
        String reverseStr;

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a string to reverse :");
        originalStr = scanner.nextLine();

        StringBuffer stringBuffer = new StringBuffer(originalStr);
        reverseStr = stringBuffer.reverse().toString();

        System.out.println("Reverse string : " + reverseStr);
    }
}

Here,

  • It asks the user to enter a string. The string is stored in originalStr variable.
  • It created one StringBuffer variable by passing the string to StringBuffer constructor.
  • reverseStr is the reversed string created by using the reverse() method, which returns one StringBuffer, and applying toString to the result.
  • The final line is printing the reversed string.

You might also like: