Created
January 28, 2019 23:16
-
-
Save parzibyte/d147a6c7753a64d0e4e5e62f8e0eb6a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Comprobar si es palíndromo en Java | |
@author parzibyte | |
*/ | |
public static boolean esPalindromo(String cadena) { | |
// Convertir a minúscula y quitar espacios " ", puntos "." y comas "," | |
// También remplazar palabras con acento p.ej. í => i | |
cadena = cadena.toLowerCase().replace("á", "a").replace("é", "e").replace("í", "i").replace("ó", "o") | |
.replace("ú", "u").replace(" ", "").replace(".", "").replace(",", ""); | |
// Invertir la cadena, y si es igual que la original entonces | |
// son palíndromos | |
String invertida = new StringBuilder(cadena).reverse().toString(); | |
// Para invertir recomiendo ver | |
// https://parzibyte.me/blog/2019/02/20/invertir-cadena-string-java/ | |
return invertida.equals(cadena); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment