Check if Two Strings are Anagram in Java

Exercise:

Write a Java Program to check if two strings are anagram.

1.Check for stored strings

Click Here to View the Solution!
import java.util.Arrays;

public class Anagram {
    public static void main(String[] args) {
        String str1 = "coding";
        String str2 = "gindoc";

        // check if length is same
        if(str1.length() == str2.length()) {

            // strings  char array
            char[] charArray1 = str1.toCharArray();
            char[] charArray2 = str2.toCharArray();

            // sorting the char array
            Arrays.sort(charArray1);
            Arrays.sort(charArray2);

            // if sorted char arrays are same then the string is anagram
            boolean result = Arrays.equals(charArray1, charArray2);
            if(result) {
                System.out.println(str1 + " and " + str2 + " are anagram.");
            }
            else {
                System.out.println(str1 + " and " + str2 + " are not anagram.");
            }
        }
    }
}
Click Here to View the Output!
coding and gindoc are anagram.
Click Here to View the Explanation!
  • This programs deals with the task of checking whether two string values tend to be each other’s anagram or not.
  • For this purpose, we employed a utility Array provided by java.
  • Initially, two strings are declared in the main method.
  • After that, we check whether these strings have equal number of characters or not, using length() method.
  •  Then we move onto converting the contents of these strings into an array of characters using toCharArray() method of Array.
  • Next step is to call the sort() method of Array, which arranges the contents of both character arrays in ascending order.
  • Later we check if the characters of both arranged arrays are same or not, by using equals() method.
  • Here equals() method is case-sensitive.
  • The result is assigned to a boolean variable; in this case values tend to be anagrams.

2.Check For User-input strings

Click Here to View the Solution!
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter first String: ");
        String str1 = input.nextLine();
        System.out.print("Enter second String: ");
        String str2 = input.nextLine();
        if(str1.length() == str2.length()) {
            char[] charArray1 = str1.toCharArray();
            char[] charArray2 = str2.toCharArray();
            Arrays.sort(charArray1);
            Arrays.sort(charArray2);
            boolean result = Arrays.equals(charArray1, charArray2);
            if(result) {
                System.out.println(str1 + " and " + str2 + " are anagram.");
            }
            else {
                System.out.println(str1 + " and " + str2 + " are not anagram.");
            }
            input.close();
        }
    }
}
Click Here to View the Output!
Enter first String: listen                                                                       
Enter second String: silent                                                                      
listen and silent are anagram.                                                                  
                                
Click Here to View the Explanation!
  • This program helps in checking whether two strings are anagrams or not by taking input from the user.
  • In this case we import two java utilities, Array and Scanner, as we intend to take values from the user.
  • To carry out this task, first instantiate an object of Scanner class.
  • After that display a message indicating the user to enter the first string value by calling nextLine() method; that value is assigned to a string variable.
  • Then the second value is taken from the user by invoking the same method.
  • The rest of the process is same as the previous example i.e checking the length of these strings. If they tend to be of same length, then they are converted into character arrays.
  • Furthermore, the contents are sorted in an ascending order using sort() method.
  • Later, equals() method of Array i.e. case-sensitive also, checks if the arrays carry the same characters or not.
  • In this case, values are anagrams of each other.