-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelloWorld_Recursion.java
39 lines (28 loc) · 1.13 KB
/
HelloWorld_Recursion.java
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
29
30
31
32
33
34
35
36
37
38
39
public class HelloWorld_Recursion {
public static String recursivePrintStringMultipleLines(String str, int count) {
if (count == 0)
return str;
// decrement the counter each time
return str + "\n" + recursivePrintStringMultipleLines(str, --count);
}
public static String recursivelyDecomposeString(String str) {
if (str.length() == 0)
return "";
return str + "\n" + recursivelyDecomposeString(str.substring(0, str.length() - 1));
}
public static String recursivelyGrowString(String str, int counter) {
if (str.length() == counter)
return str;
return str.substring(0, counter) + "\n" + recursivelyGrowString(str, ++counter);
}
public static void main(String args[]) {
// 1. Print "Hello, World" multiple times using an integer parameter
int printCount = 4;
System.out.println(recursivePrintStringMultipleLines("Hello, World!", printCount));
System.out.println("\n\n");
// 2. Decompose "Hello, World" character by character
System.out.println(recursivelyDecomposeString("Hello, World!"));
// 3. Grow "Hello, World" character by character
System.out.println(recursivelyGrowString("Hello, World!", 0));
}
}