Hi,
I am trying to write a java program to check for palindrome...but I am not getting the correct o/p...pls help me in figuring out the prob...here is the code:
public class Palindrome { public static void main(String args) { // TODO Auto-generated method stub String str = "candidate"; Palindrome obj = new Palindrome(); if(obj.isPalindrome(str)) System.out.print(" Palindrome"); else System.out.print("not Palindrome"); } public boolean isPalindrome(String str) { int left = 0; // index of leftmost unchecked char int right = str.length() -1; // index of the rightmost while (left < right) { // continue until they reach center if (str.charAt(left) != str.charAt(right)) { return false; // if chars are different, finished } left++; // move left index toward the center right--; // move right index toward the center } return true; // if finished, all chars were same } }
public class TestforPalindrome { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Palindrome obj = new Palindrome(); System.out.println ("mom is a palindrome? "+ obj.isPalindrome("mom")); System.out.println ("apple pie is a palindrome? "+ obj.isPalindrome("Apple Pie")); System.out.println ("poor dan is in a droop is a palindrome? "+ obj.isPalindrome("poor dan is in a droop")); System.out.println ("poor don is in a droop is a palindrome? "+ obj.isPalindrome("Poor Don is in a droop")); System.out.println ("Madam, I'm Adam is a palindrome? "+ obj.isPalindrome("madam Im adam")); System.out.println ("A man, a plan, a canal, Panama is a palindrome? "+ obj.isPalindrome("a man, a plan, a canal, panama")); } }