Sample Program:-
public class SampleProgram {
public static void main(String[] args) {
String str = "abcde";
reverseStringRecurively(str);
}
private static void reverseStringRecurively(String str) {
if (str.length() == 1) {
System.out.print(str);
} else {
reverseStringRecurively(str.substring(1, str.length()));
System.out.print(str.substring(0, 1));
}
}
}
Output:-
edcba
Enjoy Coding
No comments:
Post a Comment