java - PalindromeChecker comparTo -
i trying make palindrome (the inverse of word word self e.g. tacocat) checker in java. use code:
private static void palindroomchecker(string sword){ char[] arrcword=sword.tochararray(); char[] arrcdrow=new char[arrcword.length]; for(int i=0;i<arrcword.length;i++) for(int j=arrcdrow.length-1;j>=0;j--) arrcdrow[j]=arrcword[i]; string sdrow=new string(arrcdrow); if(sword.compareto(sdrow)==0) system.out.println(sword); else system.out.println("false"); }
for reason keep printing false. reason there no palindrome's not tacocat.
you need index. should work.
private static void palindroomchecker(string sword) { char[] arrcword = sword.tochararray(); char[] arrcdrow = new char[arrcword.length]; int = 0; (int j = arrcdrow.length - 1; j >= 0; j--) arrcdrow[j] = arrcword[i++]; string sdrow = new string(arrcdrow); if (sword.compareto(sdrow) == 0) system.out.println(sword); else system.out.println("false"); }
Comments
Post a Comment