import java.util.Scanner;
public class Pailindrome {
public static void main(String[] args) {
Scanner sc1 = new Scanner(System.in);
System.out.println("Please enter a word");
String ori = sc1.nextLine();
isPailindrome(ori);
if (isPailindrome(ori))
System.out.println(ori + "is a Pailindrome");
else
System.out.println(ori + "is NOT a Pailindrome");
}
public static boolean isPailindrome(String ori) {
int i = 0;
int j = ori.length() - 1;
while (i < j) {
if (ori.charAt(i) != ori.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}
The code works perfectly I'm just confused how I will get it to work irrespective of the case
inputted by the user. For example aBba is a palindrome but It says it's not in the code I've done. I
would like any help if possible thanks.
4 Réponses :
Prenez l'entrée et appelez TOUPPER (); De cette façon, lorsque vous vérifiez si c'est un palindrome, tous les caractères sont majuscules.
String ori = scr.nextLint(); if(isPalindrome(ori.toUpperCase())) //do something
Vous pouvez convertir toutes les lettres en minuscule avant de démarrer le traitement.
Vous pouvez écrire votre propre fonction ou utiliser tolowercase () code> fonction de chaîne. p> import java.util.Scanner;
public class Pailindrome {
public static void main(String[] args) {
Scanner sc1 = new Scanner(System.in);
System.out.println("Please enter a word");
String ori = sc1.nextLine();
ori = ori.toLowerCase();
isPailindrome(ori);
if (isPailindrome(ori))
}
System.out.println(ori + "is a Pailindrome");
} else {
System.out.println(ori + "is NOT a Pailindrome");
}
}
public static boolean isPailindrome(String ori) {
int i = 0;
int j = ori.length() - 1;
while (i < j) {
if (ori.charAt(i) != ori.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
Convertissez tous les cas en minuscules / majuscules avant de vérifier le palindrome p>
ispailindrome (ori.tolowercase ()); code> p>
Zoom avant des deux extrémités et ajustez le boîtier au besoin.
Il suffit de convertir la chaîne en majuscule ou en minuscule, puis comparez.
ori = ori.touppercase () code>.Est-ce que cela répond à votre question? Comment comparer le caractère Ignorer le cas dans les types primitifs