Java - Palindromic string inversion

Asked

Viewed 1,794 times

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;

}

}

3 answers

3


Follows a small code I made according to the situation reported:

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 first we need to treat the white spaces of st, which will be our main string. Once this is done, simply store the value of the inverted string itself (I used the StringBuilder to simplify the example).

Finally, we can perform the comparison between the two String’s. If they are equal, it is a palindrome, otherwise, it is not.

With this you can base and implement your own solution.

Good studies!

  • 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 :)

  • 1

    I edited the answer to that situation.

  • Okay, thanks for the help.

1

Remove all spaces from the original sentence before reversing.

You can do this with the following command:

entrada = entrada.replace(" ", "");

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 ;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.