|
|
| import java.util.Scanner; |
| /** |
| * @author nuwan.n.bandara |
| * @since 2/18/15 |
| * @version 1.0 |
| */ |
| public class PalindromeCheck { |
| public static void main(String[] args) { |
| Scanner scanner = new Scanner(System.in); |
| System.out.println("Enter the String for check:"); |
| String str = scanner.nextLine(); |
| System.out.println(str + " is" + ((isPalindrome(str) ? " palindrome." : " not palindrome."))); |
| } |
| private static boolean isPalindrome(String str) { |
| if (str == null || str.length() == 0) { |
| return false; |
| } |
| else { |
| int maxIdx = str.length() - 1; |
| int mid = str.length() / 2; |
| char[] strChar = str.toCharArray(); |
| for (int i = 0; i < mid; i++) { |
| if (strChar[i] != strChar[maxIdx - i]) { |
| return false; |
| } |
| } |
| } |
| return true; |
| } |
| } |
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home