1 public class LoopPalindromes
2 {
3 public static void main(String[] args)
4 {
5 String sentence1 = "Madam, I'm Adam!";
6 System.out.println(sentence1);
7 System.out.println("Palindrome: " + isPalindrome(sentence1));
8 String sentence2 = "Sir, I'm Eve!";
9 System.out.println(sentence2);
10 System.out.println("Palindrome: " + isPalindrome(sentence2));
11 }
12
13 /**
14 Tests whether a text is a palindrome.
15 @param text a string that is being checked
16 @return true if text is a palindrome, false otherwise
17 */
18 public static boolean isPalindrome(String text)
19 {
20 int start = 0;
21 int end = text.length() - 1;
22 while (start < end)
23 {
24 char first = Character.toLowerCase(text.charAt(start));
25 char last = Character.toLowerCase(text.charAt(end));
26 if (Character.isLetter(first) && Character.isLetter(last))
27 {
28 // Both are letters.
29 if (first == last)
30 {
31 start++;
32 end--;
33 }
34 else
35 {
36 return false;
37 }
38 }
39 if (!Character.isLetter(last)) { end--; }
40 if (!Character.isLetter(first)) { start++; }
41 }
42 return true;
43 }
44 }