Java.lang.String do not have any inbuilt method as Reverse() to reverse a String, rather we can use Java.lang.StringBuffer which has an inbuilt Reverse() method.
Let’s see the implementation –
Using String –
String str = "qavbox"; String strRev=""; //System.out.println(str.Reverse()); //no reverse method for(int i=str.length()-1; i>=0; i--){ strRev = strRev + str.charAt(i); }
We are taking individual characters from the str and printing in reverse order.
Using StringBuffer –
StringBuffer sbf = new StringBuffer("qavbox"); System.out.println("Stringbuffer - " + sbf); System.out.println("Reverse stringbuffer - " + sbf.reverse());
Hope this helps!