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
34 lines (29 loc) · 678 Bytes
/
Recurse.java
File metadata and controls
34 lines (29 loc) · 678 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
29
30
31
32
33
34
/**
* 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();
}
}