package com.inforhomex;
import static java.lang.System.out;
import static java.lang.System.err;
public class Main{
public static void main(String[] args){
testMock();
}
public static void testMock(){
String cadena = "anita lava la tina";
cadena = quitoEspacios(cadena);
int tam = cadena.length();
out.println("Entrada: "+cadena);
if(esPalindroma(cadena,tam)){
out.println("Es palindroma");
}
}
static boolean esPalindroma(String cadena, int tam){
boolean resultadoPal = false;
String cadenaTmp = "";
out.println("Tam: "+tam);
for(int i = tam; i-- >= 0;){
try{
char cx = cadena.charAt(i);
cadenaTmp += String.valueOf(cx);
out.println(""+cadenaTmp);
}catch(StringIndexOutOfBoundsException ex){
err.println("");
}
}
if(cadenaTmp.equals(cadena)){
resultadoPal = true;
}
return resultadoPal;
}
public static String quitoEspacios(String cadena){
String espacio = "";
for(int j = 0; j < cadena.length();j++){
char c = cadena.charAt(j);
if(!" ".equals(String.valueOf(c))){
espacio += String.valueOf(c);
}
}
return espacio;
}
}
Palindroma en Java
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.