lundi 31 mai 2021

Palindrome program , i don't understand a few parts

String word;

int z;

int y = 0;

int i = 0;

char letter;

Scanner input = new Scanner(System.in);

System.out.print("Enter a word: ");

word = input.nextLine();

word = word.replaceAll("\s+", ""); // ik the use of replace all and its syntax clearly but i can't understand "\s+" , "" part

word = word.toLowerCase();

z = word.length()-1; // why do word.length()-1? what's the reason of doing so

//after this basically i dont understand the whole loop so can you please explain

while (i <= z) {

if ((letter = word.charAt(i)) == (letter = word.charAt(z-i))){
    y += 1;
}
i += 1;

}

//and also this if else statement , please elaborate

if (y == (z+1)) {

System.out.println("The word IS a palindrome");

} else {

System.out.println("The word is NOT a palindrome");

}

} }

Aucun commentaire:

Enregistrer un commentaire