forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecurse.java
More file actions
106 lines (95 loc) · 2.7 KB
/
Copy pathRecurse.java
File metadata and controls
106 lines (95 loc) · 2.7 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Recursion exercise.
*/
public class Recurse {
/**
* Returns the first character of the given String.
*/
public static char first(String s) {
return s.charAt(0);
}
/**
* Returns all but the first letter of the given String.
*/
public static String rest(String s) {
return s.substring(1);
}
/**
* Returns all but the first and last letter of the String.
*/
public static String middle(String s) {
return s.substring(1, s.length() - 1);
}
/**
* Returns the length of the given String.
*/
public static int length(String s) {
return s.length();
}
/**
* Returns the letters of a string, one on each line.
*/
public static void printString(String s){
if(length(s) == 1){ //base case
System.out.println(first(s));
} else{ //recursive case
System.out.println(first(s));
printString(rest(s));
}
}
/**
* Returns the letters of a string backward,
* one on each line.
*/
public static void printBackward(String s){
if(length(s) == 1){ //base case
System.out.println(first(s));
} else{ //recursive case
printString(rest(s));
System.out.println(first(s));
}
}
/**
* Returns a string with letters in reverse order,
* given an initial string.
*/
public static String reverseString(String s){
if(length(s) == 1){ //base case
String r = "";
r += first(s);
return r;
} else{ //recursive case
return reverseString(rest(s)) + first(s);
}
}
/**
* Returns true whether a string is a palindrome,
* false otherwise.
*/
public static boolean isPalindrome(String s){
if(length(s) == 1){
return true;
} else if(length(s) == 2) {
return first(s) == first(rest(s));
} else {
boolean isTruePalindrome = isPalindrome(middle(s));
if(first(s) == first(reverseString(s))){
return isTruePalindrome;
} else {
return false;
}
}
}
public static void main(String[] args){
System.out.println(first("banana"));
System.out.println(rest("banana"));
System.out.println(middle("banana"));
System.out.println(length("banana"));
printString("banana");
printBackward("banana");
String backwards = reverseString("coffee");
System.out.println(backwards);
System.out.println(isPalindrome("palindromeemordnilap"));
System.out.println(isPalindrome("otto"));
}
}