-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.java
More file actions
28 lines (25 loc) · 866 Bytes
/
Recursion.java
File metadata and controls
28 lines (25 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Recursion {
public static void main(String[] args){
//problem1:Print a string in reverse order.
String first = "first program";
//printStringInReverse(first);
printReverse("xyzabc".toCharArray());
}
private static void printStringInReverse(String str) {
if (str == null || str.isEmpty()) return;
//System.out.println(str.substring(1));
printStringInReverse(str.substring(1));
System.out.print(str.charAt(0));
}
private static void printReverse(char [] str) {
helper(0, str);
}
private static void helper(int index, char [] str) {
if (str == null || index >= str.length) {
return;
}//xyzabc
System.out.println(str[index] + " " + index);
helper(index + 1, str);
System.out.print(str[index]);
}
}