// package chapter7; /** * Title: Chapter 7, "Strings" * Description: Examples for Chapter 7 * Copyright: Copyright (c) 2000 * Company: Armstrong Atlantic State University * @author Y. Daniel Liang * @version 1.0 */ // import chapter2.MyInput; public class PalindromeIgnoreNonAlphanumeric { /** Main method */ public static void main(String[] args) { System.out.print("Enter a string: "); String s = MyInput.readString(); System.out.println("Ignoring non-alphanumeric characters, is " + s + " a palindrome? " + isPalindrome(s)); } /** Return true if a string is a palindrome*/ public static boolean isPalindrome(String s) { // Create a new string be eliminating non-alphanumeric chars String s1 = filter(s); // Create a new string that is the reversal of s1 String s2 = reverse(s1); // Compare is the reversal is the same as the original string return s2.equals(s1); } /** Create a new string be eliminating non-alphanumeric chars*/ public static String filter(String s) { // Create a string buffer StringBuffer strBuf = new StringBuffer(); // Examine each char in the string to skip alphanumeric char for (int i=0; i