Java - Palindrome string inversion

3

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;

}

}

    
asked by anonymous 09.02.2016 / 15:37

3 answers

3

Here is a small code that I did according to the reported situation:

public class Main {

    public static void main(String[] args) {

        String st = "socorram me subi no onibus em marrocos";
        String stAux = st.replaceAll("\s+", "");

        String stReverse = new StringBuilder(stAux).reverse().toString();
        String stReverseAux = new StringBuilder(st).reverse().toString();

        System.out.println("Original: " + st);
        System.out.println("Invertido: " + stReverseAux);

        if (stAux.equals(stReverse))
            System.out.println("É um palíndromo");
        else
            System.out.println("Não é um palíndromo");

    }

}

Note that we first need to handle the whitespace of st , which will be our main String. Once this is done, just store the value of the String itself (I used StringBuilder to simplify the example).

Finally, we can compare the two String's. If they are the same, it is a palindrome, otherwise it is not.

With this you can base and implement your own solution.

Good studies!

    
09.02.2016 / 16:01
1

Remove all spaces from the original phrase before reversing.

You can do this with the following command:

entrada = entrada.replace(" ", "");
    
09.02.2016 / 15:51
0

Why not use recursion? It's so simple:

public static void main(String[] args) 
{
    System.out.println(palindromo("reviver", 0));
    System.out.println(palindromo("arara", 0));
    System.out.println(palindromo("rodador",0));
    System.out.println(palindromo("matheus", 0));
    System.out.println(palindromo("socoz", 0));
}

static boolean palindromo(String  a, int s)
{
    if(a.length() == s)
        return true ;
    if(a.charAt(s) == a.charAt(a.length() - ++s))
        return palindromo(a,s);
    else return false ;
}
    
08.09.2018 / 13:34