on Leave a Comment

Java Program to Check Whether Two Strings are Anagram or Not

This java program checks whether two strings are anagram or not. Anagram means producing a new word using rearranging of letters of a word, length of letters must be same in both new and previous words. In this java program, user will enter two strings and the code of checking anagram runs and show whether both strings are anagram or not. 

Java Program Check Anagram or Not

Example 1:

In this program, first we check the length of both strings, if length is same then runs the if blocks, otherwise run the else block. In else block, print "Strings are not Anagram". And in the if block, we take first letter of first string and compare it with all letters of second string. If the letter not matches, print "Not Anagram" and exit the program. After checking first letter of first string, check another letter. When all letter string 1 matches with string 2, prints "Strings are Anagram".

import java.util.Scanner;
public class CheckAnagram{
	public static void main(String[] args) {
		String str1, str2;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter First String: ");
		str1 = sc.next();
		System.out.print("Enter Second String: ");
		str2 = sc.next();

		int i, j, found=0;
		if(str1.length() == str2.length()){
			for(i=0; i<str1.length(); i++)
			{
				for(j=0; j<str1.length(); j++){
					if(str1.charAt(i) == str2.charAt(j)){
						found =1;
					}
				}
				if(found==0){
					System.out.println("Strings are not Anagram");
					System.exit(0);
				}		
			}
			System.out.println("Strings are Anagram");	
		}
		else{
			System.out.println("Strings are not Anagram");
		}	
	}
}

Output:

Enter First String: triangle
Enter Second String: integral
Strings are Anagram

Enter First String: alpha
Enter Second String: beta
Strings are not Anagram

Example 2:

In this program, first we check the length of both strings and if length is same if block runs, otherwise else block runs. In else block, print "Strings are not Anagram". And in if block, sort both array and directly compares string 1 with string 2. If both strings matches print "Strings are Anagram", otherwise print "String are not Anagram".

import java.util.Scanner;
import java.util.Arrays;
public class CheckAnagram{
	public static void main(String[] args) {
		String str1, str2;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter First String: ");
		str1 = sc.next();
		System.out.print("Enter Second String: ");
		str2 = sc.next();
        
        char c1[] = str1.toCharArray();
        char c2[] = str2.toCharArray();
        str2 = str2.replaceAll("\\s", "");
        Arrays.sort(c1);
        Arrays.sort(c2);
		if(str1.length() == str2.length()){
			if(Arrays.equals(c1, c2))
				System.out.println("Strings are Anagram");
			else
				System.out.println("Strings are not Anagram");
		}
		else{
			System.out.println("Strings are not Anagram");
		}	
	}
}

Output:

Enter First String: triangle
Enter Second String: integral
Strings are Anagram

Enter First String: alpha
Enter Second String: beta
Strings are not Anagram




0 comments:

Post a Comment