Share Pastebin
Guest
Public paste!

RICHARD PALINDROME

By: a guest | Mar 18th, 2010 | Syntax: Java 5 | Size: 2.99 KB | Hits: 163 | Expires: Never
Copy text to clipboard
  1. package examples;
  2.  
  3. /**
  4.  * This class represents a single sentence. It is based on an example from
  5.  * section 13.2 of Horstmann's Big Java, 3rd ed.
  6.  *
  7.  * @author Cay Horstmann
  8.  */
  9. public class Sentence
  10. {
  11.  
  12.         private final String text;
  13.  
  14.         /**
  15.          * Creates a sentence object for the given string.
  16.          *
  17.          * @param text
  18.          */
  19.         public Sentence(String text)
  20.         {
  21.                 this.text = text;
  22.         }
  23.  
  24.         /**
  25.          * Main entry point for example.
  26.          *
  27.          * @param args
  28.          *            ignored
  29.          */
  30.         public static void main(String[] args)
  31.         {
  32.                 String str = "Go hang a salami, I'm a lasagna hog.";
  33.                 Sentence sent = new Sentence(str);
  34.                 System.out.println(sent.isPalindrome());
  35.         }
  36.  
  37.         /**
  38.          * Checks whether this sentence is a palindrome. Palindromic sentences are
  39.          * considered to be those the read the same forward or backward, ignoring
  40.          * case, punctuation, and spaces.
  41.          *
  42.          * Examples:
  43.          *
  44.          * new Sentence("deified");
  45.          *
  46.          * new Sentence("I prefer Pi");
  47.          *
  48.          * new Sentence("A man, a plan, a canal -- Panama!");
  49.          *
  50.          * new Sentence("Madam, I'm Adam");
  51.          *
  52.          * new Sentence("Go hang a salami, I'm a lasagna hog.");
  53.          *
  54.          * @return true iff this sentence is a palindrome
  55.          */
  56.         public boolean isPalindrome()
  57.         {
  58.                 return this.isPalindrome(0, this.text.length() - 1);
  59.         }
  60.  
  61.         /**
  62.          * Checks whether this sentence is a palindrome. Palindromic sentences are
  63.          * considered to be those the read the same forward or backward, ignoring
  64.          * case, punctuation, and spaces. (recursive helper)
  65.          *
  66.          * @param start
  67.          * @param end
  68.          * @return true or false if the first/last letters match
  69.          */
  70.         public boolean isPalindrome(int start, int end)
  71.         {
  72.                 int length = end - start + 1;
  73.                 if (length < 1)
  74.                 {
  75.                         return true;
  76.                 }
  77.                 char first = Character.toLowerCase(this.text.charAt(start));
  78.                 char last = Character.toLowerCase(this.text.charAt(end));
  79.                 boolean firstIsValid = Character.isLetter(first);
  80.                 boolean lastIsValid = Character.isLetter(last);
  81.                 if (firstIsValid && lastIsValid)
  82.                 {
  83.                         if (first == last)
  84.                         {
  85.                                 return this.isPalindrome(start + 1, end - 1);
  86.                         }
  87.                         else
  88.                         {
  89.                                 return false;
  90.                         }
  91.                 }
  92.                 if (!firstIsValid)
  93.                 {
  94.                         return this.isPalindrome(start + 1, end);
  95.                 }
  96.                 if (!lastIsValid)
  97.                 {
  98.                         return this.isPalindrome(start, end - 1);
  99.                 }
  100.                 System.out.println("Shouldn't get here.");
  101.                 return false;
  102.         }
  103.  
  104.         /**
  105.          * @return a NEW sentence object whose text is the reverse of this one
  106.          */
  107.         public Sentence reverse()
  108.         {
  109.                 return this.reverse(this.toString());
  110.         }
  111.         /**
  112.          * @param string
  113.          * @return a NEW sentence object with the last character at the end
  114.          */
  115.         public Sentence reverse(String string)
  116.         {
  117.                 if (string.length() <= 1)
  118.                 {
  119.                         return new Sentence(string);
  120.                 }
  121.                 else
  122.                 {
  123.                         String endChar = string.substring(string.length()-1,string.length());
  124.                         String restOfString = string.substring(0, string.length() -1);
  125.                         return new Sentence(endChar + this.reverse(restOfString));
  126.                 }
  127.         }
  128.  
  129.         @Override
  130.         public String toString()
  131.         {
  132.                 return this.text;
  133.         }
  134. }