checking for string is palindrome or not.
checking for string is palindrome or not.
You can use the following test cases to verify your code logics.
ID | Input | Ouput |
---|---|---|
1 | a | true |
2 | ab | false |
3 | abc | false |
4 | abcd | false |
5 | abbc | true |
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) { // } }
Here is the answer to the question above.
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; } }