Java Program – Check two strings are Anagram

Introduction

The Two string will be anagram to each other if and only if they contain the same number of characters (order of the characters doesn’t matter). We have written this program using Java technologies, please find the code in the below section.


Java Program


package com.kw.sample;

import java.util.Arrays;
import java.util.Scanner;

/**
 * This Program helps to check provided String are Anagram or Not!
 * 
 * @author dsahu1
 * 
 */
public class KWAnagramString {

	// Instantiate Object Scanner to take Input from the user
	private static Scanner scan = new Scanner(System.in);

	/**
	 * This method helps to execute the Program/
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("Enter the First String");
		// Attribute to contain String1
		String str1 = scan.nextLine();

		System.out.println("Enter the Second String");
		// Attribute to contain String2
		String str2 = scan.nextLine();

		// Call method to check Anagram for Input String
		boolean isAnagramString = checkAnagramString(str1, str2);
		// Check String(s) are Anagram or Not
		if (isAnagramString) {
			// Print the output
			System.out.println("The Provided String(s) " + str1 + " & " + str2
					+ " are Anagram!");
		} else {
			// Print the output
			System.out.println("The Provided String(s) " + str1 + " & " + str2
					+ " are not a Anagram!");
		}
	}

	/**
	 * This method helps to check two String as anagram or not.
	 * 
	 * @param str1
	 * @param str2
	 * @return true if String(s) are Anagram and false if Not.
	 */
	public static boolean checkAnagramString(String str1, String str2) {
		// Attribute to hold boolean value
		boolean isAnagramString = false;

		// Attribute to hold valid string result
		boolean validString = str1 != null && str2 != null && !"".equals(str1)
				&& !"".equals(str2);

		// Validate the Anagram String Logic
		if (!validString || (str1.length() != str2.length())) {

			isAnagramString = false;
		} else {
			// Compare String(s)
			int compare = str1.compareTo(str2);

			if (compare == 0 || sortString(str1).equals(sortString(str2))) {
				isAnagramString = true;
			}
		}
		return isAnagramString;
	}

	/**
	 * This method to helps you to sort the String.
	 * 
	 * @param str
	 * @return sort the string based on character
	 */
	public static String sortString(String str) {

		// Convert String to Array of character
		char tempArray[] = str.toCharArray();

		// use API to sort the array by character
		Arrays.sort(tempArray);

		// Convert Array of character to String
		return new String(tempArray);
	}
}


Output


String str1 = “TRIANGLE”;
String str2 = “INTEGRAL”;
The Provided String(s) TRIANGLE & INTEGRAL are Anagram!


String str1 =”LISTEN”;
String str2 = “SILENT”;
The Provided String(s) TRIANGLE & INTEGRAL are Anagram!


String str1 =”Dharmendra”;
String str2 = “Dharmendrer”;
The Provided String(s) Dharmendra & Dharmender are not a Anagram!



Leave a Reply

Your email address will not be published. Required fields are marked *