0 évaluation0% ont trouvé ce document utile (0 vote)
27 vues2 pages
String has only one character so it / / is a palindrome by definition. If the first and last characters of the string are both letters, the string is a palindrome. The substring created by dropping the first character in the string is also a palidrome.
String has only one character so it / / is a palindrome by definition. If the first and last characters of the string are both letters, the string is a palindrome. The substring created by dropping the first character in the string is also a palidrome.
Droits d'auteur :
Attribution Non-Commercial (BY-NC)
Formats disponibles
Téléchargez comme TXT, PDF, TXT ou lisez en ligne sur Scribd
String has only one character so it / / is a palindrome by definition. If the first and last characters of the string are both letters, the string is a palindrome. The substring created by dropping the first character in the string is also a palidrome.
Droits d'auteur :
Attribution Non-Commercial (BY-NC)
Formats disponibles
Téléchargez comme TXT, PDF, TXT ou lisez en ligne sur Scribd
private String pal; public Palindromee(String initPal) { pal = initPal.toUpperCase(); } public boolean isPalindrome() { if (pal.length() <= 1) { // String has only one character so it // is a Palindrome by definition. System.out.println("es palindrome"); return true; } // Get the first and last characters of the String. char first = pal.charAt(0); char last = pal.charAt(pal.length()-1); if (Character.isLetter(first) && Character.isLetter(last)) { // The first and last characters are both letters.. if (first != last) { // The first and last letters are different // so the string is not a Palindrome. System.out.println("NO es palindrome"); return false; // BASE CASE. } else { // The first and last characters are both letters, // and they are both the same. So, the string is // a palindrome if the substring created by dropping // the first and last characters is a palindrome. Palindromee sub = new Palindromee( pal.substring(1,pal.length()-1)); return sub.isPalindrome(); // RECURSIVE CASE. } } else if (!Character.isLetter(first)) { // The first character in the string is not a letter. // So the string is a palindrome if the substring created // by dropping the first character is a palindrome. Palindromee sub = new Palindromee(pal.substring(1)); return sub.isPalindrome(); // RECURSIVE CASE. } else { // The last character in the string is not a letter. // So the string is a palindrome if the substring created // by dropping the last character is a palindrome. Palindromee sub = new Palindromee( pal.substring(0,pal.length()-1)); return sub.isPalindrome(); // RECURSIVE CASE. } } public static void main(String[] args) { Palindromee p1 = new Palindromee("Madam"); System.out.println(p1.isPalindrome()); Palindromee p2 = new Palindromee("ala"); System.out.println(p2.isPalindrome()); Palindromee p3 = new Palindromee("hola"); System.out.println(p3.isPalindrome()); Palindromee p4 = new Palindromee("ala"); System.out.println(p4.isPalindrome()); } }