Anagram Program in Java | String Anagram Program in Java

 

What is Anagram Program in Java?

The two strings are called as Anagram of each other if they contain the same characters either arranged in sequence or not it is not necessary.

How to check two Strings are Anagram or not?

To check whether the given two strings are Anagram of each other or not the compiler will ask the user to enter the two strings to check. After the input given by the user, the program will start executing are check whether the strings are Anagram or not.

After executing the compiler will display the output. The two strings are Anagram if and only if characters in that strings are the same sequence is not mandatory.

For Example abcd and dcba, creative and reactive, course and source are Anagram of each other.

Anagram Algorithm:

1 Step: Declare two Strings.

2 Step: Find out the length of two Strings (Strings are not anagram if the length of strings is not the same).

3 Step: Even if the lengths are equal the two strings should be in lowercase because it makes easy to check.

4 Step: Now sort the characters in the strings.For, sorting convert the strings into a character array.

5 Step: After converting character array must be sorted.

6 Step: In the last step, the Anagram is checked.

There are different ways to check the Anagram program in Java we will see it one by one.

1. Anagram Program in Java Using Sorting

To check whether the strings are anagram of each other we have to use two java methods i.e sort( ) and equals( ).

First, we have to remove all the white spaces between the characters of the string. Then all the characters will be converted to the lower case so the case will be ignored while checking.

Now the strings are converted into a character array and then sorted using sort( ) method. After sorting we will compare both the strings using equals( ) method.

If it returns true then the two strings are an anagram of each other else not anagram of each other.

In this program, we have declared two strings abcd and cabd. Now, the program will check whether the two Strings are Anagram or not.

Java

Output:

2. Using Iterative Method

In this method, we check whether each character of the first string is situated in the second string. If the character is found then we remove that character from the second string and proceed to the next character.

And if the character is not found in the second string then the for loop is terminated. Then, we consider the given two strings are not an anagram of each other.

Java

Output:


3. Anagram Program in Java Using StringBuilder

This method is as same as the above method but, only the difference is that we ave used the java inbuilt method deletecharAt( ).

deletecharAt( ) method deletes the character from the second string if that character is present in the second string.

Java

Output:

anagram program in java

4. Using HashMap

To construct a HashMap we need Key and the Value. So, in this method, we will construct one object of HashMap where the character will be Key and the character occurrence as the value.

At first, we will increment the character count by 1. If the character is present in the first string and then decrement by 1 if that character is present in the second string.

In this way, we check the character count of the entire string. If any character count is not equal to zero, then both the string is not an anagram of each other.

Java

Output:

using sorting

5. Anagram Program in Java Using X-OR

In this program, we will use the logic of XOR. Here, XOR returns bit by bit digits of XOR.

If XOR returns 1 then the bits are different and if XOR returns 0 then the bits are same.

C

Output:

using XOR method

6. Using ArrayList

In this method, each character of the string is added to the list and then whether both the lists are equal are checked.

C

Output:

using arraylist method

Conclusion:

So in this lesson, we have learned about the anagram program in C. If you have any doubt then please feel free to share it in the comment box given below.

No comments