StringBuilder reverse() in Java

reverse(): This method is available in java.lang.StringBuilder class of Java.

Syntax:

StringBuilder java.lang.StringBuilder.reverse()

Causes this character sequence to be replaced by the reverse of the sequence.

Let n be the character length of this character sequence(not the length in char values) Then the character at index k in the new character sequence is equal to the character at index n-k-1 in the old character sequence.

Parameters: NA

Returns: a reference to this object which is the reverse of the original object.

Exceptions: NA

For Example:

StringBuilder str = new StringBuilder("Hello World")

str.reverse() = > It returns dlroW olleH.

Approach

Java

public class Reverse {
    public static void main(String[] args) {

        StringBuilder str = new StringBuilder("Hello World");

        System.out.println(str.reverse());
    }
}

Output:

dlroW olleH

No comments:

Post a Comment