eriezelagera

Java Exercises

Feb 4th, 2015
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1.  
  2. import java.io.IOException;
  3.  
  4. /**
  5.  *
  6.  * @author Erieze
  7.  */
  8. public class Main {
  9.  
  10.     /**
  11.      * Check if the given {@code text} is a Palindrome String.
  12.      * <p>
  13.      * Punctuation and whitespace are ignored.
  14.      *
  15.      * @param text A String
  16.      * @return True if text is palindrome, otherwise false
  17.      */
  18.     private static boolean isPalindrome(String text) {
  19.         return removePunc(text).equalsIgnoreCase(
  20.                 removePunc(new StringBuilder(text).reverse().toString()));
  21.     }
  22.    
  23.     /**
  24.      * Removes punctuation from String.
  25.      *
  26.      * @param text A String
  27.      * @return String without punctuation
  28.      */
  29.     private static String removePunc(String text) {
  30.         return text.replaceAll("\\s|(?![@,&])\\p{Punct}", "");
  31.     }
  32.    
  33.     public static void main(String[] args) throws IOException {
  34.         final String text = "Are we not pure? \"No sir!\" Panama's moody Noriega brags. \"It is garbage!\" Irony dooms a man; a prisoner up to new era.";
  35.         System.out.println("isPalindrome(" + text + ")? " + isPalindrome(text));
  36.        
  37.         final String s = "QWERTY";
  38.         String result = "";
  39.        
  40.         for (char ch : s.toCharArray()) {
  41.             if (ch == 'Q')
  42.                 result += "A";
  43.             else if (ch == 'W')
  44.                 result += "B";
  45.             else if (ch == 'E')
  46.                 result += "C";
  47.             else if (ch == 'R')
  48.                 result += "D";
  49.             else if (ch == 'T')
  50.                 result += "E";
  51.             else if (ch == 'Y')
  52.                 result += "F";
  53.         }
  54.        
  55.         System.out.println(Integer.parseInt(result, 16));
  56.     }
  57.    
  58. }
Advertisement
Add Comment
Please, Sign In to add comment