Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. import p2utils.Queue;
  2. import p2utils.Stack;
  3.  
  4.  
  5. public class Palindrome
  6. {
  7.     public static void main(String args[])
  8.     {
  9.         if(args.length < 1 )
  10.         {
  11.             System.out.println("java Palindrome <frase ou palavra>");
  12.         }
  13.  
  14.         String sentence = "";
  15.         for(String s : args)
  16.         {
  17.             sentence += " " + s;
  18.         }
  19.  
  20.  
  21.         if(isPalindrome(sentence))
  22.         {
  23.             System.out.println("A palavra " + sentence + " é um palíndromo");
  24.         }
  25.         else
  26.         {
  27.             System.out.println("A palavra '" + sentence + "' não é um palíndromo");
  28.         }
  29.     }
  30.  
  31.     public static boolean isPalindrome(String sentence)
  32.     {
  33.         Queue<String> q = new Queue<String>();
  34.         Stack<String> s = new Stack<String>();
  35.  
  36.         for(char c : sentence.toCharArray())
  37.         {
  38.             if(Character.isLetterOrDigit(c))
  39.             {
  40.                 q.in(String.valueOf(Character.toLowerCase(c)));
  41.                 s.push(String.valueOf(Character.toLowerCase(c)));
  42.             }
  43.         }
  44.  
  45.         do
  46.         {
  47.             if(s.top().compareTo(q.peek()) != 0) return false;
  48.             s.pop();
  49.             q.out();
  50.         }
  51.         while(!s.isEmpty());
  52.          
  53.         return true;
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement