Saturday, 31 March 2018

How to pprint 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


No comments:

Post a Comment