Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (24 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (322 Blogs)
  • Spring Framework (8 Blogs)

An easy way to implement Anagram Program in Java

Published on Sep 26,2019 11.9K Views

Wonders can be done with Strings, especially in Java. In this article, we will see how to implement the Anagram program in Java in the following order:


What is an Anagram?

An Anagram is a word which is formed by rearranging or shuffling of letters in another word, the most important property in Anagram is that all the letters have to be used only once. For example, let’s take the popular anagram, LISTEN is an anagram of SILENT.


In this Anagram Program in Java, we will look into some of the possible ways to check if two Strings are Anagram or Not.


Anagram Program In Java Using sort() and equals() Methods

First, we clean the input by removing all white spaces from the given two strings and change the case of all characters of both the strings to lower case so that the case of both input strings will be ignored. After cleaning the input strings, we convert them to character array and sort them using sort() method of java.util.Arrays class.

After sorting, we compare both the arrays using equals() method of the same Array class. This method will return true if both arrays have the same set of characters. Below is the complete anagram program using sort() and equals() methods.

public class AnagramProgram
static void isAnagram(String s1, String s2)
//Removing all white spaces from s1 and s2
String copyOfs1 = s1.replaceAll("s", "");
String copyOfs2 = s2.replaceAll("s", "");
//Initially setting status as true
boolean status = true;
if(copyOfs1.length() != copyOfs2.length())
//Setting status as false if copyOfs1 and copyOfs2 doesn't have same length
status = false;
//Changing the case of characters of both copyOfs1 and copyOfs2 and converting them to char array
char[] s1Array = copyOfs1.toLowerCase().toCharArray();
char[] s2Array = copyOfs2.toLowerCase().toCharArray();
//Sorting both s1Array and s2Array
//Checking whether s1Array and s2Array are equal
status = Arrays.equals(s1Array, s2Array);
System.out.println(s1+" and "+s2+" are anagrams");
System.out.println(s1+" and "+s2+" are not anagrams");
public static void main(String[] args)
isAnagram("Mother In Law", "Hitler Woman");
isAnagram("keEp", "peeK");
isAnagram("SiLeNt CAT", "LisTen AcT");
isAnagram("Debit Card", "Bad Credit");
isAnagram("School MASTER", "The ClassROOM");
isAnagram("DORMITORY", "Dirty Room");
isAnagram("Toss", "Shot");
isAnagram("joy", "enjoy");




Check if Two Strings Are Anagram Using Array

This is the simplest of all methods. After getting the strings from the user and we need to first remove all the white space and convert them into the lower case for a non-case sensitive comparison. Now convert them into a character array and sort them alphabetically. Just compare both arrays has the same elements.

package com.javainterviewpoint;

import java.util.Arrays;

import java.util.Scanner;

public class AnagramChecker


    public static void main(String[] args)


        Scanner scanner = new Scanner(;

        // Getting the input string from the user

        System.out.print("Enter the First String : ");

        String s1 = scanner.nextLine();

        System.out.print("Enter the second String : ");

        String s2 = scanner.nextLine();

        if(checkAnagram(s1, s2))

            System.out.println(s1+" and "+s2+" are Anagrams");


            System.out.println(s1+" and "+s2+" are NOT Anagrams");



    public static boolean checkAnagram(String s1, String s2)


        // Remove all the white space

        s1 = s1.replaceAll("s", "");

        s2 = s2.replaceAll("s", "");

        // Check if both length matches

        if(s1.length() != s2.length())

            return false;



            // Convert both Strings into lower case and into Character Array

            char[] arr1 = s1.toLowerCase().toCharArray();

            char[] arr2 = s2.toLowerCase().toCharArray();

            // Sort both Character Array



            // Check if both arrays are equal

            return (Arrays.equals(arr1, arr2));




With this, we come to an end of this Anagram Program in Java article. I hope you got an understanding of what exactly is an anagram and how to write an anagram program in Java. 

Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “Anagram Program in Java” blog and we will get back to you as soon as possible.


Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.