Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class test {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- while (true) {
- String text1 = sc.nextLine();
- if (text1.equals("x"))
- break;
- String text2 = sc.nextLine();
- System.out.println(isAnagram(text1, text2));
- }
- }
- static boolean isAnagram(String a, String b) {
- if (a.length() != b.length()) {
- return false;
- }
- a = a.toLowerCase();
- b = b.toLowerCase();
- String A = alphabetical(a);
- String B = alphabetical(b);
- if (A.compareTo(B) == 0) {
- // System.out.println("Anagrams");
- return true;
- } else {
- // System.out.println("Not Anagrams");
- return false;
- }
- }
- static String alphabetical(String a) {
- char[] temp = a.toCharArray();
- for (int j = 0; j < a.length(); j++) {
- for (int i = 0; i < a.length() - 1 - j; i++) {
- if (temp[i] > temp[i + 1]) {
- char p = temp[i + 1];
- temp[i + 1] = temp[i];
- temp[i] = p;
- }
- }
- }
- String A = new String(temp);
- System.out.println("Alphabetical String: " + A);
- return A;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment