Advertisement
FacuValverdi

EdD-TP2-PTO2-Palindromos

Sep 23rd, 2022 (edited)
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. package tp2;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5. import java.util.Stack;
  6.  
  7. public class eje2 {
  8.  
  9.     public static boolean palindromo(Stack<Character> pila, ArrayList<Character> arreglo) {
  10.         boolean aux=true;
  11.         for(int i=0;i<arreglo.size();i++){
  12.             if(pila.peek()!=arreglo.get(i)){
  13.                 aux=false;
  14.             }
  15.             pila.pop();
  16.         }
  17.         return aux;
  18.     }
  19.    
  20.     public static void main(String[] args) {
  21.         // TODO Auto-generated method stub
  22.         Scanner lectura= new Scanner(System.in);
  23.         System.out.println("Ingrese una frase");
  24.         String frase=lectura.nextLine();
  25.         ArrayList<Character> arregloPalindromo = new ArrayList<Character>();
  26.         Stack<Character> pilaPalindromo =new Stack<Character>();
  27.        
  28.         for(int i=0;i<frase.length();i++) {
  29.             if(frase.charAt(i)!=' ') {
  30.                 pilaPalindromo.push(Character.toLowerCase(frase.charAt(i)));
  31.                 arregloPalindromo.add(Character.toLowerCase(frase.charAt(i)));
  32.             }
  33.         }
  34.         if(palindromo(pilaPalindromo,arregloPalindromo)) {
  35.             System.out.println("La frase '"+frase+"' es PALINDROMO.");
  36.         }else{
  37.             System.out.println("La frase "+frase+" NO es PALINDROMO.");
  38.         }
  39.        
  40.     }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement