import java.nio.file.DirectoryStream.Filter;
import javax.swing.JOptionPane;
public class PalindromeIgnoreNonAlphanumeric {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s=JOptionPane.showInputDialog("Enter a string:");
String output="Ignoring nonalphanumeric characters,\nis"
+s+"a palindrome?"
+isPalindrome(s);
JOptionPane.showMessageDialog(null, output);
}
//判读是否是回文串
public static boolean isPalindrome(String s){
String s1=filter(s);//去除非字母和数字
String s2=reverse(s1);//字符串反转
return s2.equals(s1);
}
public static String filter(String s){
StringBuffer strBuf=new StringBuffer();
for(int i=0;i
{
if(Character.isLetterOrDigit(s.charAt(i))){
strBuf.append(s.charAt(i));
}
}
return strBuf.toString();
}
public static String reverse(String s){
StringBuffer stringBuffer=new StringBuffer(s);
stringBuffer.reverse();
return stringBuffer.toString();
}
}