Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.fhnw.claudemartin;
- public class SomeClass {
- public static void main(final String[] args) {
- test("", true);
- test("12345", false);
- test("racecar", true);
- test("Madam", false);
- test("abcdefgfedcba", true);
- test("abcdefggfedcba", true);
- // one symbol in two chars (surrogate pair):
- final String clef = "\uD834\uDD1E";
- test(clef + clef, true);
- }
- static void test(final String a, final boolean expected) {
- final boolean actual = testPalindrome(a);
- (actual == expected ? System.out : System.err).format("'%s' => %s%n", a, actual);
- }
- /**
- * is the given string a palindrome?
- *
- * Note: this doesn't work with surrogate pairs.
- *
- * @param str
- * the string to be tested
- * @return true if it's a palindrome.
- */
- public static boolean testPalindrome(final String str) {
- final int last = str.length() - 1;// last valid index
- if (-1 == last) // empty string
- return ????;
- final int m = ????; // last index to be checked. first is 0.
- for (int i = 0; i <= m; i++)
- if (???? != ????)
- return ????;
- return ????;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement