Java - checking for string is palindrome or not.

checking for string is palindrome or not.

Description

checking for string is palindrome or not.

Test cases

You can use the following test cases to verify your code logics.

ID Input Ouput
1a true
2ab false
3abc false
4abcdfalse
5abbctrue

Code template

Cut and paste the following code snippet to start solve this problem.

public class Main {
  public static void main(String[] args) {
    System.out.println(test("a"));
    System.out.println(test("ab"));
    System.out.println(test("abc"));
    System.out.println(test("abcd"));
    System.out.println(test("abbc"));
  }

  public static boolean test(String str) {
//
  }
}

Answer

Here is the answer to the question above.

Demo

public class Main {
  public static void main(String[] args) {
    System.out.println(test("a"));
    System.out.println(test("ab"));
    System.out.println(test("ab"));
    System.out.println(test("abc"));
    System.out.println(test("abcd"));
    System.out.println(test("abbc"));
  }/*w w  w.  j  a  v a2 s .c o  m*/

  public static boolean test(String str) {
    int len = str.length();
    if (len == 1) {
      return true;
    }
    if ((len == 2) && str.charAt(0) == str.charAt(1)) {
      return true;
    }
    if (len > 2) {
      for (int i = 0; i < len / 2 - 1; i++) {
        if ((str.charAt(len / 2 - i) != str.charAt((len / 2 + i)))&& ((len / 2 + i) <= len)) {
        }
        return true;
      }
    }
    return false;
  }
}