Olá,
sei que há vários fóruns com palíndromo, mas gostaria de tirar dúvida sobre meu próprio código...
Palíndromo é o número ou palavra ou frase que lido de trás pra frente é exatamente igual se lido de frnte pra trás...
Exemplo:
11211
ou
ana
ou
amoreroma
...
Meus códigos compilam mas não funcionam...
Se alguém puder ajudar a corrigi-los ficarei grato!
class Testando{
boolean ehPalindromoNRO(int n){
boolean ok = false;
int nroAlg = 1;
while(n/10 != 0){
n = n/10;
nroAlg++;
}
//verifica qtos algarismos possui n
int[] alg = new int[nroAlg - 1];
int vezes = 0;
for(int i = nroAlg - 1; i >= 0; i--){
for(int d = 0; d < nroAlg - i; d++){
int[] dez = new int[nroAlg - i];
dez[d] = 10;
vezes = 10*dez[d];
}
alg[i] = n % vezes;
}
//cada algarismo vale "tanto"
int i = 0;
int j = nroAlg - 1;
while(i < nroAlg && j >= 0){
if(alg[i] == alg[j]){
ok = true;
}
i++;
j--;
}
return ok;
}
//compara o 1º com o último, o 2º com o penúltimo...
boolean ehPalindromo(String texto) {
boolean ok = false;
int i = 0;
int j = texto.length() - 1;
while(i < texto.length() && j >= 0) {
char c = texto.charAt(i);
char d = texto.charAt(j);
if(c == d){
ok = true;
}
i++;
j--;
}
return ok;
}
}
Obrigado!