Java anagram program
In this post, you will learn how to write an anagram program using the Java programming language.
An anagram of a string is another string that contains the same characters, only the order of the characters can be different. For example, triangle and integral are anagram strings. Both strings contain the same set of characters.
So, if we need to check if two strings are an anagram or not, we will have to check if both strings contain the same characters or not.
Java program for anagram strings
In the given Java program, we have taken two input strings and stored them in two separate variables. Next, we have sorted both strings alphabetically and compared both strings to see if they are equal or not. If equal, they are anagrams. Otherwise not.
// JAVA program to check whether two strings
// are anagrams of each other
import java.util.Arrays;
public class AnagramString {
static void checkAnagram(String str1, String str2) {
String s1 = str1.replaceAll("\\s", "");
String s2 = str2.replaceAll("\\s", "");
boolean result = true;
// Check length of strings
if (s1.length() != s2.length()) {
result = false;
} else {
// Convert strings to char array
char[] ArrayStr1 = s1.toLowerCase().toCharArray();
char[] ArrayStr2 = s2.toLowerCase().toCharArray();
// Sort the char array
Arrays.sort(ArrayStr1);
Arrays.sort(ArrayStr2);
result = Arrays.equals(ArrayStr1, ArrayStr2);
}
if(result) {
System.out.println(s1 + " and " + s2 + " are anagrams");
} else {
System.out.println(s1 + " and " + s2 + " are not anagrams");
}
}
public static void main(String[] args) {
// Calling function
checkAnagram("Race", "Care");
checkAnagram("Silent", "Listen");
}
}
Output of the above code:
Race and Care are anagrams
Silent and Listen are anagrams
Anagram program in Java using for loop
In the given Java program, we ask the user to provides two string input. We use for loop to iterate each character of the first string and remove the particular character from the second string when it is found. When there are no characters left in the second string, then both the strings are an anagram.
import java.util.Scanner;
public class AnagramString
{
public static void main(String[] args)
{
Scanner scr = new Scanner(System.in);
// Getting the input string from the user
System.out.print("Enter the first string: ");
String str1 = scr.nextLine();
System.out.print("Enter the second string: ");
String str2 = scr.nextLine();
if (checkAnagram(str1, str2))
System.out.println(str1 + " and " + str2 + " are Anagrams.");
else
System.out.println(str1 + " and " + str2 + " are not Anagrams.");
scr.close();
}
public static boolean checkAnagram(String str1, String str2)
{
// Remove all the white space and convert to lower case
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();
// Check length of both strings
if(str1.length() != str2.length())
return false;
else
{
for (int i = 0; i < str1.length(); i++)
{
for (int j = 0; j < str2.length(); j++)
{
if (str1.charAt(i) == str2.charAt(j))
{
str2 = str2.substring(0, j) + str2.substring(j + 1);
break;
}
}
}
if(str2.length() == 0)
{
return true;
} else
{
return false;
}
}
}
}
Output of the above code:
Enter the first string: SILENT
Enter the second string: LISTEN
SILENT and LISTEN are Anagrams.
Anagram program in Java using XOR
In the given Java program, we have used the bitwise XOR operator to check the anagram strings. Bitwise XOR returns 1 if the digits are different and 0 if the digits are the same. If the result is 0, then the strings are anagrams.
import java.util.Scanner;
public class AnagramProgram
{
public static void main(String[] args)
{
Scanner scr = new Scanner(System.in);
System.out.print("Enter the first string: ");
String str1 = scr.nextLine();
System.out.print("Enter the second string: ");
String str2 = scr.nextLine();
if (checkAnagram(str1, str2))
System.out.println(str1 + " and " + str2 + " are Anagrams");
else
System.out.println(str1 + " and " + str2 + " are NOT Anagrams");
scr.close();
}
public static boolean checkAnagram(String str1, String str2)
{
// Remove all the white space,
// convert string in lower case & character array
char[] arr1 = str1.replaceAll("\\s", "").toLowerCase().toCharArray();
char[] arr2 = str2.replaceAll("\\s", "").toLowerCase().toCharArray();
if (arr1.length != arr2.length)
return false;
int xor = 0;
for (int i = 0; i < arr1.length; i++)
{
xor ^= arr1[i] ^ arr2[i];
}
return xor == 0? true: false;
}
}
Output of the above code:
Enter the first string: reactive
Enter the second string: creative
reactive and creative are Anagrams
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java