anagram-program-in-java

Simple Anagram Program in Java Using String in Just 10 Steps

Write a Simple Anagram Program in Java Using String. We are going to take two strings and compare and check are they 2 strings are anagrams or not. We are also going to know what is an anagram, we are going to solve this program in java using two strings, for that, we have to compare two strings for check whether they are an anagram or not.

Anagram Program in Java Using String- Table of Content

  • What is Anagram
  • Anagram Definition
  • Example of an Anagrams
  • How to Find two Strings are Anagram- Step by Step Guide
  • Source Code for String Anagram Program in Java
  • Java Anagram Program Output
  • Anagram Program Explanation Diagram.

What is Anagram

Anagram means to check two strings have the same characters or not. In this checking order of the characters does not mandatory to be a same, for example, the first string is “DELL” and the second String is “LLED” both have the same characters so they are Anagram. Below is the Simple Anagram Program in Java Using String in Just 10 Steps.

Anagram Definition

Two Strings Contain the same Characters, Whether Strings order does not need to be same. All characters of one String should be present in the second string to be an Anagram.

Anagram Definition

Example of an Anagram

  1. String s1 = “DELL” ;
  2. String s2 = “LLED”;

These two strings are Anagram. Cause both have Same characters.

  1. String s3 = “RACE”;
  2. String s4 = “ARCER”;

These strings are not Anagram. Cause both don’t have Same characters. String3 has 4 characters and String4 has 5 characters.

Simple Anagram Program in Java Step by Step

So As we know now for being a String Anagram number of character should be same on both strings. Now we know our first condition. Now we just need to compare the first string characters to second string, and if all characters meet than Strings are Anagram else Not.

Finally there are the list of 10 steps which may help you to understand the flow of the program, so just followed the Simple Anagram Program in Java Using String in Just 10 Steps.

Step 1: First take one class CkeckAnagramString and import Arrays package and here package is nothing but a group of class.

Step 2: Than take one main method inside this class we will call the Anagram method by passing two strings in an Anagram method.

Here we can call an Anagram method more than one time with passing another string for checking any other two strings are an anagram or not. Example Anagram(“Computer”, “DeskTop”);

Step 3: This passes a string to store in string1 or string2 variables than the stored string remove all white space by using the replace method.

We can see here what is the //s  it indicates the single whitespace is removed and convert the string into the lower case. After the replace the string stored in the same variable.

Step 4: After that take one variable with any name, this variable type must be Boolean.

Step 5: Take one if statement inside this statement provides one condition. string1 is not equal to string2. Here str.length() through we are finding the first string length and compares the length of the second string.

If both strings are not equal then if part executed than the status value becomes false. If both strings are equal then else part will be executed.

Step 6: Inside else part, we take two charArray names of this  Array are Arrays1 and Arrays2. Use of this Array is storing the characters for storing we convert this first passing string “keep” into charArray by using of the toCharArray method().

Simple Anagram Program in Java Using String; The string character converts into the lower case by using of toLowerCase() method and storing in ArrayS1 after that passing second string also follows the same approach after that store in ArrayS2.

Steps 7: After that sort an ArrayS1 and ArrayS2 by the using of the sort() method.

Step 8: ArrayS1 and ArrayS2 store Characters are compared by using of equals method because of equals method compare the string character-wise if both strings are equal than its return true which is stored in the status variable.

Step 9: Hence If both strings contain characters then this staus variable store true after comparing both strings.

Last Step 10: Take one if statement and pass this if statement block status variable, if it is true than its executed if part and print the string, are Anagram otherwise else part executed then print strings are not an Anagram.

Code for String Anagram Program in Java

import java.util.Scanner;

public class Anagram
{
    public static void main(String[] input)
    {
        String string1, string2;
        int l, length1, length2;
		int i, j, count=0, temp=0;
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter the First String: ");
        string1 = scan.nextLine();
		
        System.out.print("Enter the Second String: ");
        string2 = scan.nextLine();
        
        length1 = string1.length();
        length2 = string2.length();
		
        if(length1 == length2)
        {
            l = length1;
            for(i=0; i<l; i++)
            {
                count = 0;
                for(j=0; j<l; j++)
                {
                    if(string1.charAt(i) == string2.charAt(j))
                    {
                        count = 1;
                        break;
                    }
                }
                if(count == 0)
                {
                    temp = 1;
                    break;
                }
            }
            if(temp == 1)
            {
                System.out.print("Strings are not Anagram\n");
            }
            else
            {
                System.out.print("Strings are Anagram\n");
            }
        }
        else
        {
            System.out.print("For Anagram: Both Strings Must have the same number of Characters");
        }
    }
}


Anagram Program Output

Output of Simple Anagram Program in Java Using String

Anagram Program Explanation Diagram

Anagram Program Explanation Diagram
Anagram Program First Diagram
Diagram Anagram Program Explanation
Anagram Program Second Diagram

Angram Test Inputs

Input: 1  String1 = “SAMSUNG” and String2 = “AMSSUNG”

Output: Anagram

Input: 2 String1 = “Shubham” and String2 = “Shubhamtirole”;

Output:- Strings are not Anagram

I hope you like Simple Anagram Program in Java Using String and the comparison of the program.

3 Comments

  1. Raju Bhaiya August 10, 2019
  2. PAWAN Jadham July 31, 2019
    • Ghanendra Yadav August 2, 2019