anagram java program

Let’s learn anagram java program.

anagram java program

string anagram in java

Two strings can be an anagram string if characters of one string contain same characters of another string by regrouping the characters. For example,

anagram example in java:

String1 = “silent”;

String2 = “listen”;

In the above example String1 and String2 are two different strings. By regrouping the characters of String1 we have another string String2. Hence above two strings are anagram.

To understand below anagram program first have knowledge in strings and arrays. Now let’s see string anagram program in java.

import java.util.Arrays;
public class StringAnagramProgram
{
   public static void main(String[] args)
   {
      String strOne = "Silent";
      String strTwo = "Listen";
      strOne = strOne.toLowerCase();
      strTwo = strTwo.toLowerCase();
      // checking if two strings length are same
      if(strOne.length() == strTwo.length())
      {
         // converting strings to char array
         char[] charOne = strOne.toCharArray();
         char[] charTwo = strTwo.toCharArray();
         // sorting character array
         Arrays.sort(charOne);
         Arrays.sort(charTwo);
         // if sorted character arrays are same then the string is anagram
         boolean output = Arrays.equals(charOne, charTwo);
         if(output)
         {
            System.out.println(strOne + " and " + strTwo + " are anagram.");
         }
         else
         {
            System.out.println(strOne + " and " + strTwo + " are not anagram.");
         }
      }
      else
      {
         System.out.println(strOne + " and " + strTwo + " are not anagram.");
      }
   }
}


Output:

silent and listen are anagram.

In the above java program strings strOne and strTwo are converted to lowercase because java is casesensitive and characters ‘S’ and ‘s’ are two different characters.


anagram program taking input from user

Now let’s learn to print anagrams by taking input from user using nextLine() method of Scanner class and check if strings are anagram.

import java.util.Arrays;
import java.util.Scanner;
public class PrintAnagrams
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter first string: ");
      String strOne = sc.nextLine();
      System.out.print("Please enter second string: ");
      String strTwo = sc.nextLine();
      // checking if length are same
      if(strOne.length() == strTwo.length())
      {
         // converting strings to character array
         char[] charOne = strOne.toCharArray();
         char[] charTwo = strTwo.toCharArray();
         // sorting character array
         Arrays.sort(charOne);
         Arrays.sort(charTwo);
         // if sorted character arrays are same then the string is anagram
         boolean result = Arrays.equals(charOne, charTwo);
         if(result)
         {
            System.out.println(strOne + " and " + strTwo + " are anagram.");
         }
         else
         {
            System.out.println(strOne + " and " + strTwo + " are not anagram.");
         }
      }
      else
      {
         System.out.println(strOne + " and " + strTwo + " are not anagram.");
      }
      sc.close();
   }
}


Output:

Please enter first string: triangle
Please enter second string: integral
triangle and integral are anagram.

Please enter first string: arun
Please enter second string: kumar
arun and kumar are not anagram.


string anagram in java using function

In the below program we are going to write a function to check if two strings are anagrams java.

import java.util.Arrays;
public class AnagramUsingFunction
{
   public static void main(String[] args)
   {
      char[] ch1 = { 'e', 'l', 'v', 'i', 's' };
      char[] ch2 = { 'l', 'i', 'v', 'e', 's' };
      if(checkAnagram(ch1, ch2))
      {
         System.out.println("two strings are anagram.");
      }
      else
      {
         System.out.println("two strings are not anagram.");
      }
   }
   static boolean checkAnagram(char[] ch1, char[] ch2)
   {
      int len1 = ch1.length;
      int len2 = ch2.length;
      // if length of both strings is not equal it cannot be anagram
      if(len1 != len2)
         return false;
      // sorting both strings
      Arrays.sort(ch1);
      Arrays.sort(ch2);
      // compare sorted strings
      for(int a = 0; a < len1; a++)
         if(ch1[a] != ch2[a])
            return false;
         return true;
   }
}


Output:

two strings are anagram.