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

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -