Anagram Program in Java – Check Two Strings are Anagram or Not

Java program to check two strings are anagrams or not. We are also going to know what is an anagram, we are going to solve this program in java using two strings, for that, we have to compare two strings for check whether they are an anagram or not.

Two Strings are Anagram in Java or Not- Table of Content

  • What is Anagram
  • Anagram Definition
  • Example of an Anagrams
  • How to Find two Strings are Anagram- Step by Step Guide
  • Java Program to Check two Strings are Anagram or Not
  • Java Program Output
  • Programs Explnaination by Pictorial Representation.

What is Anagram

Anagram means to check two strings have the same characters or not. In this checking order of the characters does not mandatory to be a same, for example, the first string is “DELL” and the second String is “LLED” both have the same characters so they are Anagram.

Anagram Definition

Two Strings Contain the same Characters, Whether Strings order does not need to be same. All characters of one String should be present in the second string to be an Anagram.

Example of an Anagrams

  1. String s1 = “DELL” ;
  2. String s2 = “LLED”;

These two strings are Anagram. Cause both have Same characters.

  1. String s3 = “RACE”;
  2. String s4 = “ARCER”;

These strings are not Anagram. Cause both don’t have Same characters. String3 has 4 characters and String4 has 5 characters.

Anagram Program in Java- Step by Step Solution

So As we know now for being a String Anagram number of character should be same on both strings.

Now we know our first condition. Now we just need to compare the first string characters to second string, and if all characters meet than Strings are Anagram else Not.

Step 1:- First take one class CkeckAnagramString and import Arrays package and here package is nothing but a group of class.

Step 2:- Than take one main method inside this class we will call the Anagram method with passing two strings in an Anagram method.

Here we can call an Anagram method more than one time with passing another string for checking any other two strings are an anagram or not. Example Anagram(“Computer”, “DeskTop”);

Step 3:- This passes a string to store in string1 or string2 variables than the stored string remove all white space by using the replace method.

We can see here what is the //s  it indicates the single whitespace is removed and convert the string into the lower case.

After the replace the string stored in the same variable.

Important Steps

Step 4:- After that take one variable with any name, this variable type must be Boolean.

Step 5:- Take one if statement inside this statement provides one condition. string1 is not equal to string2. Here str.length() through we are finding the first string length and compares the length of the second string.

If both strings are not equal then if part executed than the status value becomes false. If both strings are equal then else part will be executed.

Step 6:- Inside else part, we take two charArray names of this  Array are Arrays1 and Arrays2. Use of this Array is storing the characters for storing we convert this first passing string “keep” into charArray by using of the toCharArray method().

The string character converts into the lower case by using of toLowerCase() method and storing in ArrayS1 after that passing second string also follows the same approach after that store in ArrayS2.

Steps 7:- After that sort an ArrayS1 and ArrayS2 by the using of the sort() method.

Step 8:- ArrayS1 and ArrayS2 store Characters are compared by using of equals method because of equals method compare the string character-wise if both strings are equal than its return true which is stored in the status variable.

Step 9:- Hence If both strings contain characters then this staus variable store true after comparing both strings.

Last Step 10:- Take one if statement and pass this if statement block status variable, if it is true than its executed if part and print the string, are Anagram otherwise else part executed then print strings are not an Anagram.

Java Program to Check two Strings are Anagram or Not

import java.util.Scanner;

public class Anagram
    public static void main(String[] input)
        String string1, string2;
        int l, length1, length2;
		int i, j, count=0, temp=0;
        Scanner scan = new Scanner(;
        System.out.print("Enter the First String: ");
        string1 = scan.nextLine();
        System.out.print("Enter the Second String: ");
        string2 = scan.nextLine();
        length1 = string1.length();
        length2 = string2.length();
        if(length1 == length2)
            l = length1;
            for(i=0; i<l; i++)
                count = 0;
                for(j=0; j<l; j++)
                    if(string1.charAt(i) == string2.charAt(j))
                        count = 1;
                if(count == 0)
                    temp = 1;
            if(temp == 1)
                System.out.print("Strings are not Anagram\n");
                System.out.print("Strings are Anagram\n");
            System.out.print("For Anagram: Both Strings Must have the same number of Characters");

Program Output

Strings are Anagram or not Java Program Output

Program Explanation through Diagram:-

Strings are Anagram or Not Diagram Flow Chart
Strings are Anagram or Not Diagram Explain

User Inputs:-

Input:- 1  String1 = “SAMSUNG” and String2 = “AMSSUNG”

Output:- Anagram

Input:– 2 String1 = “Shubham” and String2 = “Shubhamtirole”;

Output:- Strings are not Anagram


  1. Raju Bhaiya August 10, 2019
  2. PAWAN Jadham July 31, 2019
    • Ghanendra Yadav August 2, 2019