StringBuffer reverse() in Java

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

Syntax:

StringBuffer java.lang.StringBuffer.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) just prior to execution of the reverse method. 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.

Returns: a reference to this object.

For Example:

StringBuffer str = new StringBuffer("Hello World")

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

Approach

Java

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

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

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

Output:

dlroW olleH

No comments:

Post a Comment