Building an Anagram checker in Java

Are you a fan of word games and puzzles? If so, get ready to crack the code with Java’s string manipulation!

In this article, we will explore how to build an anagram checker using Java’s powerful string manipulation functions.

According to Wikipedia, an anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

With Java’s string manipulation capabilities, we can efficiently solve anagram puzzles with ease. By leveraging Java’s string manipulation methods such as sorting, comparing, and iterating through characters, we can create a robust anagram solver that will help you conquer even the trickiest word puzzles.

Whether you’re a beginner or an experienced Java developer, this article will guide you through the process of building an efficient and effective anagram solver using Java.

So, get ready to crack the code and unlock a whole new level of wordplay!

What is an anagram program?

An anagram program checks if two strings are anagrams, so they have the same number of letters and the same letters.

First, let’s write the algorithm of the program.

Step 1: Give input two strings.
Step 2: Remove the spaces between words (if the string contains two or more words).
Step 3: Create an array of chars for each string.
Step 4: Sort the arrays.
Step 5: If the sorted arrays are equal, the strings are anagrams, else they aren’t anagrams.

Anagram program in Java

Now, let’s implement this algorithm in the Java program.

The first step is to write a method, which will get two Strings as input and will do all the calculations. This method will show a message in Console, so the returning type will be void.

static void checkStrings(String s1, String s2)

 

The next step is to remove white spaces if there are. The method replaceAll( ) in Java will do this task. For both strings, we will write:

s1.replaceAll("\\s", "");

 

Now let’s declare a boolean variable, which will store the result. In the beginning, we will assume that the strings are anagrams.

boolean result = true;

 

Let’s create an array of chars, and put the two strings in these arrays. This will work to check if they have the same letters. We will write such instruction for both strings :

char[] s1Ch = s1.toLowerCase().toCharArray();

 

Now, we will sort the arrays of chars. Both of them.

Arrays.sort(s1Ch);

 

The rule is: if the sorted arrays are equal, the two strings are anagrams. They contain exactly the same letters.

result = Arrays.equals(s1Ch, s2Ch);

 

The method will show a message in Console, for each cases, if they are anagrams or not.

The final version of the program Anagram Checker looks like this:

import java.util.Arrays;  
public class Anagram_Demo {  
 static void checkStrings(String s1, String s2) {  
  s1.replaceAll("\\s", "");  
  s2.replaceAll("\\s", "");  
  boolean result = true;  
  if (s1.length() != s2.length()) {  
   result = false;  
  } 
  else {  
   char[] s1Ch = s1.toLowerCase().toCharArray();  
   char[] s2Ch = s2.toLowerCase().toCharArray();  
   Arrays.sort(s1Ch);  
   Arrays.sort(s2Ch);  
   result = Arrays.equals(s1Ch, s2Ch);  
  }  
        
  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) {  
  checkStrings("keep", "Peek");   
  checkStrings("Night", "Thing");   
 }  
}  

 

Hoping this was helpful. Keep coding…

For more Java Applications Tutorials, visit Code in Java