Advertisement
RAUL-SUAREZ

tp3-casoejemplo-c

Oct 25th, 2021
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // Created by Julio Tentor <jtentor@fi.unju.edu.ar>
  3. //
  4.  
  5. /*
  6. Dada una frase determinar si la misma es o no un palíndromo haciendo uso de
  7. una pila y una cola. No se debe tener en cuenta los espacios en blanco.
  8.  
  9.  */
  10.  
  11. import java.util.Scanner;
  12. import java.util.Stack;
  13.  
  14. public class CasoEjemplo_c {
  15.  
  16.     private static Scanner scanner = new Scanner(System.in);
  17.  
  18.     public void Run() {
  19.  
  20.         System.out.println("Trabajo Práctico Nº 3 - Caso Ejemplo c)\n");
  21.  
  22.         System.out.print("Ingrese un frase: ");
  23.         String text = scanner.nextLine();
  24.         test(text);
  25.  
  26.         // from https://es.wikipedia.org/wiki/Palíndromo
  27.         test("Dábale arroz a la zorra el abad");
  28.         test("¿Son mulas o cívicos alumnos?");
  29.         test("Eva, can I stab bats in a cave?");
  30.  
  31.     }
  32.  
  33.     private void test(String text) {
  34.         System.out.println("\"" + text + "\"" +  (isPalindrome(text) ? "" : " NO ") + " es un palíndromo" );
  35.     }
  36.  
  37.     private boolean isPalindrome(String text) {
  38.  
  39.         Stack<Character> stack = new Stack<Character>();
  40.         Queue<Character> queue = new Queue<Character>(text.length());
  41.  
  42.         char ch;
  43.         int count = 0;
  44.         for(int pos = 0; pos < text.length(); ++pos) {
  45.             ch = text.charAt(pos);
  46.  
  47.             if (Character.isAlphabetic(ch)) {
  48.                 ch = Character.toUpperCase( ignoreAccentuation(ch) );
  49.                 stack.push(ch);
  50.                 queue.offer(ch);
  51.                 ++count;
  52.             }
  53.         }
  54.  
  55.         count /= 2;
  56.         while (count-- > 0) {
  57.             if ( !stack.pop().equals(queue.remove()) ) {
  58.                 return false;
  59.             }
  60.         }
  61.  
  62.         return true;
  63.     }
  64.  
  65.     private char ignoreAccentuation(char ch) {
  66.  
  67.         switch(ch) {
  68.             case 'Á' :
  69.             case 'Ä' :
  70.             case 'À' :
  71.                 return 'A';
  72.             case 'É' :
  73.             case 'Ë' :
  74.             case 'È' :
  75.                 return 'E';
  76.             case 'Í' :
  77.             case 'Ï' :
  78.             case 'Ì' :
  79.                 return 'I';
  80.             case 'Ó' :
  81.             case 'Ö' :
  82.             case 'Ò' :
  83.                 return 'O';
  84.             case 'Ú' :
  85.             case 'Ü' :
  86.             case 'Ù' :
  87.                 return 'U';
  88.  
  89.             case 'á' :
  90.             case 'ä' :
  91.             case 'à' :
  92.                 return 'a';
  93.             case 'é' :
  94.             case 'ë' :
  95.             case 'è' :
  96.                 return 'e';
  97.             case 'í' :
  98.             case 'ï' :
  99.             case 'ì' :
  100.                 return 'i';
  101.             case 'ó' :
  102.             case 'ö' :
  103.             case 'ò' :
  104.                 return 'o';
  105.             case 'ú' :
  106.             case 'ü' :
  107.             case 'ù' :
  108.                 return 'u';
  109.         }
  110.         return ch;
  111.     }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement