Advertisement
DulcetAirman

palindrome cloze

Apr 19th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. package ch.fhnw.claudemartin;
  2.  
  3. public class SomeClass {
  4.   public static void main(final String[] args) {
  5.     test("", true);
  6.     test("12345", false);
  7.     test("racecar", true);
  8.     test("Madam", false);
  9.     test("abcdefgfedcba", true);
  10.     test("abcdefggfedcba", true);
  11.  
  12.     // one symbol in two chars (surrogate pair):
  13.     final String clef = "\uD834\uDD1E";
  14.     test(clef + clef, true);
  15.   }
  16.  
  17.   static void test(final String a, final boolean expected) {
  18.     final boolean actual = testPalindrome(a);
  19.     (actual == expected ? System.out : System.err).format("'%s' => %s%n", a, actual);
  20.   }
  21.  
  22.   /**
  23.    * is the given string a palindrome?
  24.    *
  25.    * Note: this doesn't work with surrogate pairs.
  26.    *
  27.    * @param str
  28.    *          the string to be tested
  29.    * @return true if it's a palindrome.
  30.    */
  31.   public static boolean testPalindrome(final String str) {
  32.     final int last = str.length() - 1;// last valid index
  33.     if (-1 == last) // empty string
  34.       return ????;
  35.     final int m = ????; // last index to be checked. first is 0.
  36.     for (int i = 0; i <= m; i++)
  37.       if (???? != ????)
  38.         return ????;
  39.     return ????;
  40.   }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement