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
75 lines (61 loc) · 1.83 KB
/
Copy pathRecurse.java
File metadata and controls
75 lines (61 loc) · 1.83 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
/**
* Recursion exercise.
*/
import java.util.Scanner;
public class Recurse {
public static void main(String[] args) {
String s, sBackward;
Scanner stringIn = new Scanner(System.in);
System.out.print("Enter a string of text. ");
s = stringIn.nextLine();
System.out.println("First character: " + first(s));
System.out.println("Remainder of the string: " + rest(s));
System.out.println("Middle of the string: " + middle(s));
System.out.println("Length: " + length(s));
printString(s);
sBackward = printBackward(s);
isPalindrome(s, sBackward);
}
/**
* 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();
}
public static void printString(String s) {
for (int i = 0; i < s.length(); i++) {
System.out.println(s.charAt(i));
}
}
public static String printBackward(String s) {
String backward = "";
for (int i = (s.length() - 1); i >= 0; i--) {
backward += s.charAt(i);
}
return backward;
}
public static boolean isPalindrome(String s, String sBackward) {
boolean check = s.equals(sBackward);
System.out.print("Is your string a palindrome? ");
System.out.println(check);
return check;
}
}