-
Notifications
You must be signed in to change notification settings - Fork 5
/
Palindrome.java
34 lines (28 loc) · 1.08 KB
/
Palindrome.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
// Palindrome.java
// Class: cs200
public class Palindrome {
private static void printPalindromeRecursive(String s, int i) {
// first print the character, then recurse on the next character index
System.out.print(s.charAt(i));
if(i < s.length() - 1) {
// question: will i++ or ++i work on the method call below?
// or: do we want the side-effects that the ++ operators perform?
printPalindromeRecursive(s, i + 1);
}
System.out.print(s.charAt(i));
}
public static void printPalindromeStack(String s) {
// print the characters in s forwards
// load them into a new Stack()
// then pop and print them in a loop
// now you've printed forwards and backwards
// hint: you can loop over the chars of a String with a foreach loop on an array:
// for(char c : s.toCharArray())
}
public static void main(String[] args) {
printPalindromeRecursive("abc", 0);
System.out.println();
printPalindromeStack("abc");
System.out.println();
}
}