Guest User

Palindromes

a guest
Dec 9th, 2018
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package stringAndTextProcessing;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.TreeSet;
  7.  
  8. public class palindromes {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.         String text = scanner.nextLine();
  12.  
  13.         String textReversed = "";
  14.         for (int i = text.length() - 1; i >= 0; i--) {
  15.             textReversed += text.charAt(i);
  16.         }
  17.  
  18.         String[] firstCompare = text.split("[^A-za-z]+");
  19.         String[] secondCompare = textReversed.split("[^A-za-z]+");
  20.  
  21.         TreeSet<String> result = new TreeSet<>();
  22.         for (int i = 0; i < firstCompare.length; i++) {
  23.             for (int j = 0; j < secondCompare.length; j++) {
  24.                 if (firstCompare[i].equals((secondCompare[j]))) {
  25.                     result.add(secondCompare[j]);
  26.                 }
  27.             }
  28.         }
  29.  
  30.         System.out.println(result);
  31.  
  32.     }
  33. }
RAW Paste Data