mercredi 31 janvier 2018

False output of if-statement

I'm trying do check a char array if it's a palindrom.

This code works when my parameter consist of an even number of characters. If not it won't work.

E.g. "ana"

public boolean palindromTest(char[] test) {
    for (int i = 0; i<test.length-i-2;i++) {
        if (test[i] != test[test.length-i-1])
            return false;
    }
    return true;
}

Aucun commentaire:

Enregistrer un commentaire