Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tp2;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.Stack;
- public class eje2 {
- public static boolean palindromo(Stack<Character> pila, ArrayList<Character> arreglo) {
- boolean aux=true;
- for(int i=0;i<arreglo.size();i++){
- if(pila.peek()!=arreglo.get(i)){
- aux=false;
- }
- pila.pop();
- }
- return aux;
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner lectura= new Scanner(System.in);
- System.out.println("Ingrese una frase");
- String frase=lectura.nextLine();
- ArrayList<Character> arregloPalindromo = new ArrayList<Character>();
- Stack<Character> pilaPalindromo =new Stack<Character>();
- for(int i=0;i<frase.length();i++) {
- if(frase.charAt(i)!=' ') {
- pilaPalindromo.push(Character.toLowerCase(frase.charAt(i)));
- arregloPalindromo.add(Character.toLowerCase(frase.charAt(i)));
- }
- }
- if(palindromo(pilaPalindromo,arregloPalindromo)) {
- System.out.println("La frase '"+frase+"' es PALINDROMO.");
- }else{
- System.out.println("La frase "+frase+" NO es PALINDROMO.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement