I'm trying to do an exercise where I need to invert a word, phrase or number and check if it's palindrome. For words and numbers, it is already working, but for phrases it gives errors depending on what it is, for example the phrase "help me get on the bus in morocco", he considers as not being palindrome because, in inverter, the word "bus" it is "subin", without the "subi no" space, so it considers it not to be a palindrome, is there any method to solve this?
public static void main(String[] args) {
String entrada = JOptionPane.showInputDialog(null, "Digite um texto: ");
/*char[] vetEntrada = entrada.toCharArray();*/
StringBuffer StringInvertida = new StringBuffer();
StringInvertida = inverteString(entrada);
String SI = StringInvertida.toString();
char[] vetSaida = SI.toCharArray();
if (entrada.equalsIgnoreCase(SI)) {
JOptionPane.showMessageDialog(null, "É palíndromo: " + StringInvertida + " = " + entrada);
JOptionPane.showMessageDialog(null, "Vetor de verificação: ");
int x = 1;
for (char c : vetSaida) {
JOptionPane.showMessageDialog(null, "[" + x + "] " + c);
x++;
}
} else {
JOptionPane.showMessageDialog(null, "Não é palíndromo: " + entrada + " != " + StringInvertida);
JOptionPane.showMessageDialog(null, "Vetor de verificação: ");
int x = 1;
for (char c : vetSaida) {
JOptionPane.showMessageDialog(null, "[" + x + "] " + c);
x++;
}
}
}
private static StringBuffer inverteString(String entrada) {
StringBuffer SB = new StringBuffer(entrada);
SB.reverse();
return SB;
}
}