How to print string in reverse order In Java

Print string in reverse order using recursion in java

package com.jp.string.basic;

//Print reverse of a string using recursion

public class ReverseString {

public static void main(String[] args) {
String str = “java proficinecy”;
ReverseString reverseString = new ReverseString();
reverseString.printStrInReverseOrder(str);
}

private void  printStrInReverseOrder( String str ){
if ( str == null || str.length() < 1 ) {
return;
}
System.out.print(str.charAt( str.length()-1 ));
printStrInReverseOrder( str.substring( 0 , str.length() -1 ));
}

}

Output :

yceniciforp avaj

 

Leave a Reply

Your email address will not be published. Required fields are marked *

3 + 3 =