thecoderenroute

TRUE STORY

Sep 23rd, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class test {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         while (true) {
  10.             String text1 = sc.nextLine();
  11.             if (text1.equals("x"))
  12.                 break;
  13.             String text2 = sc.nextLine();
  14.             System.out.println(isAnagram(text1, text2));
  15.         }
  16.     }
  17.  
  18.     static boolean isAnagram(String a, String b) {
  19.         if (a.length() != b.length()) {
  20.             return false;
  21.         }
  22.         a = a.toLowerCase();
  23.         b = b.toLowerCase();
  24.         String A = alphabetical(a);
  25.         String B = alphabetical(b);
  26.         if (A.compareTo(B) == 0) {
  27.             // System.out.println("Anagrams");
  28.             return true;
  29.         } else {
  30.             // System.out.println("Not Anagrams");
  31.             return false;
  32.         }
  33.     }
  34.  
  35.     static String alphabetical(String a) {
  36.         char[] temp = a.toCharArray();
  37.         for (int j = 0; j < a.length(); j++) {
  38.             for (int i = 0; i < a.length() - 1 - j; i++) {
  39.                 if (temp[i] > temp[i + 1]) {
  40.                     char p = temp[i + 1];
  41.                     temp[i + 1] = temp[i];
  42.                     temp[i] = p;
  43.                 }
  44.             }
  45.         }
  46.         String A = new String(temp);
  47.         System.out.println("Alphabetical String: " + A);
  48.         return A;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment