String Anagram Program in Java

String Anagram in Java program. An anagram is a word or phrase created by rearranging the letters of another word or phrase while utilizing all of the original letters exactly once. An anagram of a string is a distinct string that comprises the same characters but in a different order. For instance, the letters “abcd” and “dabc” are an anagram of each other.

import java.util.Arrays;
class Main
{
public static void main(String[] args)
{
String str1 = "School master";
String str2 = "The classroom";
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if(str1.length() == str2.length())
{
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
boolean result = Arrays.equals(charArray1, charArray2);
if(result)
{
System.out.println(str1 + " and " + str2 + " are anagram");
}
else
{
System.out.println(str1 + " and " + str2 + " are not anagram");
}
}
else
{
System.out.println(str1 + " and " + str2 + " are not anagram");
}
}
}

 

Output:

school master and the classroom are anagram

String Anagram in Java and How it Works

An anagram is a word or phrase created by rearranging the letters of another word or phrase String Anagram in Java while utilising all of the original letters exactly once. For instance, the term anagram can be rearranged as nag a ram, as well as the words binary and adobe into dwelling.

String Anagram in Java and Purpose of Anagram

An anagram is a word play. Many words or phrases can be anagrammed by rearranging the letters. A skilled writer, on the other hand, will employ an anagram to make some sort of criticism. An anagram will be used by a writer to make a statement about the topic he is discussing.

Learning Anagram in Java is Worthy or Not

Both yes and no! Anagrams must make sense, although they can include names or places in addition to “proper words.” Nonsense words aren’t usually regarded “excellent form” in an anagram, because one of the most important aspects of an anagram is its coherence of meaning.

What is Array in Java

In Java, an array is a collection of variables that are referred to by a single variable name and an index number. An array’s items are called elements. An array’s items must all be of the same type. For example, an int array can hold int values, while a String array can hold strings. String Anagram in Java can execute the string to check the anagram in java programming.

Similar Programs