package basic; import java.util.Scanner; public class Palindrome{ public static boolean isPalindrome(String raw){ String str = ""; // 只拿raw字符串里的字母,拼接到str里 for(int i = 0; i < raw.length(); i++){ char ch = raw.charAt(i); if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){ str += ch; } } // str字母全部小写化 str = str.toLowerCase(); // 判断是否为回文 int end = str.length(); for(int i = 0; i < end/2; i++){ if(str.charAt(i) != str.charAt(end-i-1)){ return false; } } return true; } public static void main(String[] args){ Scanner scanner = new Scanner(System.in); // I prefer pi! // A man, a plan, a canal: Panama! // Madam, I am Adam. System.out.printf("Enter a message: "); String str = scanner.nextLine(); if(isPalindrome(str)){ System.out.println("Palindrome!"); }else{ System.out.println("Not a palindrome!"); } } }