Home >>Java Programs >Java Program to determine whether two strings are the anagram
In this example, we will create a java program to check whether two strings are anagram.
Two Strings can be called as the anagram if they contain the same characters.
import java.util.Arrays;
public class Main
{
public static void main (String [] args)
{
String str1="ABCD";
String str2="BDCA";
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if (str1.length() != str2.length())
{
System.out.println("Both the strings are not anagram");
}
else
{
char[] string1 = str1.toCharArray();
char[] string2 = str2.toCharArray();
Arrays.sort(string1);
Arrays.sort(string2);
if(Arrays.equals(string1, string2) == true)
{
System.out.println("Both the strings are anagram");
}
else
{
System.out.println("Both the strings are not anagram");
}
}
}
}