3
I’m trying to do an exercise where I need to invert a word, phrase or number and check if it’s palindromic. For words and numbers, it is already working, but for sentences it gives errors depending on what it is, for example the phrase "help me got on the bus in Rrocos", he considers as not being palindrome because, when reversing, the word "bus" is "going up", without the space of "I climbed on"then he considers it as not being palindromic, there is some 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;
}
}
Okay, really this way it confirms to be palindromic, only I needed to show the phrase reversed, with the spaces, but thanks anyway, I’ll try to find some method to improve it :)
– Hugo Maia
I edited the answer to that situation.
– Weslley Tavares
Okay, thanks for the help.
– Hugo Maia