Advanced   Java   Services JAVA-Praktikum Back Next Up Home
Palindrome (Lösung)
// ------------------------- Palindrom ---------------------------- \\

public class Palindrom
{
   public static boolean isOne(String pali)
   {
      return rekursion( cleanString(pali).toLowerCase() ) ;
   }

   // ----------------------- cleanString ------------------------- \\
   // übernimmt nur Buchstaben (klein und groß) und Ziffern
   public static String cleanString(String st)
   {
      StringBuffer tmp = new StringBuffer();

      for(int i=0; i<st.length() ; i++)
      {
         char ch = st.charAt(i);
         if ('0'<=ch && ch<='9' ||
             'a'<=ch && ch<='z'||'A'<=ch && ch<='Z')
          tmp.append(ch);
      }
      return tmp.toString();
   }

   // ------------------------- rekursion ------------------------- \\
   private static boolean rekursion(String pali)
   {
      if ( pali.length()==0 || pali.length()==1 )
         return true;

      if (  pali.charAt(0) == pali.charAt(  pali.length()-1 )  )
         return rekursion( pali.substring(1, pali.length()-1)  ) ;

      return false;
   }

}
Valid XHTML 1.0 Strict top Back Next Up Home