Before moving on to code solution. Let us look what Anagram is:
Anagram: Two words are anagrams of one another if their letters can be rearranged to form the other word.
//program to check whether two strings are anagrams or not.
import java.util.Arrays;
public class ANAGRAMS
{
public static void main(String args[])
{
String x = "ShE iS Madam";
String y = "iS seh mADam";
//replacing White spaces
x=x.replaceAll("","");
y=y.replaceAll("","");
//Making cases similar
x=x.toUpperCase();
y=y.toUpperCase();
//converting to array. char is datatype of array
char a[] = x.toCharArray();
char b[] = y.toCharArray();
//now sorting them
Arrays.sort(a);
Arrays.sort(b);
Boolean result=Arrays.equals(a,b);
if(result==true)
{
System.out.println("Strings are Anagram.");
}
else
{
System.out.println("Strings are not Anagram.");
}
}
}
Output:Strings are Anagram.