Java Program to Check Anagram

What is an Anagram?

An anagram is a word or phrase formed by rearranging the letters of another word or phrase. For example, “listen” and “silent” are anagrams of each other. Anagrams can be created by rearranging any or all of the letters in a word or phrase to form a new word or phrase with the same letters.

In programming, anagrams are often used in text processing and cryptography. They can be used to check whether two words or phrases have the same letters, or to create new words or phrases by rearranging the letters of existing ones.

To check whether two strings are anagrams of each other, you can count the number of occurrences of each letter in each string and compare the counts. If the counts are the same, the strings are anagrams of each other. If any letter count differs, the strings are not anagrams.

Anagrams have been studied in various fields, including mathematics, computer science, and linguistics. They have applications in cryptography, data compression, and error-correction codes.

Java Code Example using Loop to Check Anagram

Check Anagrams using Loops

Here is an example of a Java program that checks whether two strings are anagrams of each other using loops:

import java.util.Scanner;

public class AnagramChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the first string: ");
        String str1 = scanner.nextLine().toLowerCase();
        System.out.print("Enter the second string: ");
        String str2 = scanner.nextLine().toLowerCase();

        if (isAnagram(str1, str2)) {
            System.out.println("\"" + str1 + "\" and \"" + str2 + "\" are anagrams.");
        } else {
            System.out.println("\"" + str1 + "\" and \"" + str2 + "\" are not anagrams.");
        }
    }

    public static boolean isAnagram(String str1, String str2) {
        if (str1.length() != str2.length()) {
            return false;
        }

        int[] count = new int[26];

        for (int i = 0; i < str1.length(); i++) {
            count[str1.charAt(i) - 'a']++;
            count[str2.charAt(i) - 'a']--;
        }

        for (int i = 0; i < count.length; i++) {
            if (count[i] != 0) {
                return false;
            }
        }

        return true;
    }
}

Output:

Enter the first string: listen
Enter the second string: silent
"listen" and "silent" are anagrams.

In this program, the isAnagram method takes two strings as arguments and checks whether they are anagrams of each other using loops. It first checks whether the strings have the same length, as anagrams must have the same number of letters. Then, it initializes an integer array of size 26 to count the occurrences of each letter in the strings. It loops through each character in both strings and increments the count for the corresponding letter in the first string and decrements the count for the corresponding letter in the second string. Finally, it loops through the count array and checks whether all counts are zero, indicating that the strings have the same letters, and therefore are anagrams of each other.

In the main method, the program prompts the user to enter two strings, passes them to the isAnagram method, and prints the result.

Check Anagrams using Arrays

Here is an example of a Java program that checks whether two strings are anagrams of each other:

import java.util.Arrays;
import java.util.Scanner;

public class AnagramChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the first string: ");
        String str1 = scanner.nextLine().toLowerCase();
        System.out.print("Enter the second string: ");
        String str2 = scanner.nextLine().toLowerCase();

        if (isAnagram(str1, str2)) {
            System.out.println("\"" + str1 + "\" and \"" + str2 + "\" are anagrams.");
        } else {
            System.out.println("\"" + str1 + "\" and \"" + str2 + "\" are not anagrams.");
        }
    }

    public static boolean isAnagram(String str1, String str2) {
        if (str1.length() != str2.length()) {
            return false;
        }

        char[] chars1 = str1.toCharArray();
        char[] chars2 = str2.toCharArray();

        Arrays.sort(chars1);
        Arrays.sort(chars2);

        return Arrays.equals(chars1, chars2);
    }
}

Output:

Enter the first string: listen
Enter the second string: silent
"listen" and "silent" are anagrams.

In this program, the isAnagram method takes two strings as arguments and checks whether they are anagrams of each other. It first checks whether the strings have the same length, as anagrams must have the same number of letters. Then, it converts the strings to character arrays and sorts them using the Arrays.sort method. Finally, it uses the Arrays.equals method to compare the sorted character arrays and returns a boolean value indicating whether the strings are anagrams.

In the main method, the program prompts the user to enter two strings, passes them to the isAnagram method, and prints the result.

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author