Java program to check if two strings are Anagram or not

Check Two Strings are Anagram or Not

Anagrams words have the same word length, but the characters are rearranged, here we have coded a java program to find out whether two words are anagram or not
Lets take an example
  • Consider two strings elbow and below
  • Both the strings have the same length
  • Both the strings have same letters
  • But the letters are re-arranged in a way to form two different words
some more examples of anagram words are arc and car, state and taste, night and thing and  many more

 

Checking if two strings are anagram or not in C

Algorithm

  • Take string input from user and store it in the variable called “str” .
  • Call isAnagram() method by passing two string as parameter .
  • Write if condition if(s1.length()!=s2.length()) set statue = false;
  • else  convert both array to character arrya using toCharArray()
  • After that sort both the array using Arrays class sort() after that return status

Code in Java (Check Two Strings are Anagram or Not)

import java.util.Arrays;
import java.util.Scanner;
public class CheckIfTwoStringsAreAnagramAreNot {
    static boolean isAnagram(String str1 , String str2) {
    String s1 = str1.replaceAll("[\\s]", "");
    String s2 = str2.replaceAll("[\\s]", "");
    boolean status=true;

     if(s1.length()!=s2.length())
         status = false;
     else {
         char[] a1 = s1.toLowerCase().toCharArray();
         char[] a2 = s2.toLowerCase().toCharArray();
         Arrays.sort(a1);
         Arrays.sort(a2);
         status = Arrays.equals(a1, a2);
       }
       return status;
} 
   public static void main(String[] args) {
     Scanner sc = new Scanner(System.in);
     System.out.print("Enter two String :");
     String s1 = sc.next();
     String s2 = sc.next();
     boolean status = isAnagram(s1,s2);
       if(status)
          System.out.println(s1+" and "+s2+" are Anagram");
       else 
          System.out.println(s1+" and "+s2+" are not Anagram");
       }
}

Output

Enter two String : prep
repp
prep and repp are Anagram

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

7 comments on “Java program to check if two strings are Anagram or not”


  • Shraboni Sinha

    import java.util.Arrays;

    public class Anagram {
    void isAnagram(String s1, String s2){
    if(s1.length()!=s2.length()){
    System.out.println(“Not Anagram”);
    }
    char[] first = s1.toLowerCase().toCharArray();
    char[] second = s2.toLowerCase().toCharArray();

    Arrays.sort(first);
    Arrays.sort(second);
    if (Arrays.equals(first,second)){
    System.out.println(“Both Strings are Anagram”);
    }
    }
    public static void main(String[] args) {
    Anagram obj = new Anagram();
    String s1 = “shraboni”;
    String s2 = “rabhsoni”;

    obj.isAnagram(s1,s2);
    }
    }


  • Riddim

    import java.util.Arrays;
    public class angramornot {
    public static void main(String[]afsshf) {
    String a=”arc”;
    String b=”car”;
    char[]c=a.toCharArray();
    char []d=b.toCharArray();
    Arrays.sort(c);
    Arrays.sort(d);
    if(Arrays.equals(c ,d)) {
    System.out.print(“angram”);
    }else {
    System.out.print(“not sanagram”);
    }
    }

    }


  • vishakhamharnour07

    import java.util.*;
    public class Practise
    {
    public static void main(String args[])
    {
    System.out.println(“Enter a String”);
    Scanner sc=new Scanner (System.in);
    String str=sc.nextLine();
    String str1=sc.nextLine();
    char arr[]=str.toCharArray();
    char arr1[]=str1.toCharArray();
    Arrays.sort(arr);
    Arrays.sort(arr1);
    if(Arrays.equals(arr,arr1))
    {
    System.out.println(“Anagram”);
    }
    else
    {
    System.out.println(“Not a Anagram”);
    }
    }
    }


  • Ankitha

    public class Anagram {
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    String str1 = sc.nextLine();
    String str2 = sc.nextLine();
    char[] strc1=str1.toCharArray();
    char[] strc2=str2.toCharArray();
    Arrays.sort(strc1);
    Arrays.sort(strc2);int n;
    if(strc1.length>=strc2.length) {
    n=strc1.length;
    }else {
    n =strc2.length;
    }int flag=0;
    for(int i=0; i<n; i++) {
    if(strc1[i]==strc2[i]) {
    flag=1;
    }
    else {
    flag=0;
    break;
    }
    }
    if(flag==1) {
    System.out.println("yes");
    }else {
    System.out.println("no");
    }
    }

    }


  • Chelliboina

    import java.util.*;
    import java.util.Arrays;
    public class Main
    {
    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
    String s1=sc.nextLine();
    String s2=sc.nextLine();
    char c1[]=s1.toCharArray();
    char c2[]=s2.toCharArray();
    Arrays.sort(c1);
    Arrays.sort(c2);
    if(Arrays.equals(c1,c2))
    {
    System.out.print(” the strings “+s1+” “+”and”+” “+s2+” “+”are anagram”);
    }
    else
    {
    System.out.print(” the strings “+s1+” “+”and”+” “+s2+” “+”are not anagram”);

    }

    }
    }


  • saipavanreddy3105

    import java.util.*;
    public class anagram {
    public static void main(String[] args)
    {
    String s=”prepinsta”;
    String s2=”instaprep”;
    char c1[]=s.toCharArray();
    char c2[]=s2.toCharArray();
    Arrays.sort(c1);
    Arrays.sort(c2);
    int n=s.length();
    int m=s.length();
    int count=0;
    if(n==m)
    {
    for(int i=0;i<n;i++)
    {
    if(c1[i]==c2[i])
    count++;
    }

    }
    if(count==n)
    System.out.println("Anagram");
    }

    }


  • Bikash Dubey

    public class Anagram {

    static boolean checkAnagram(String str1,String str2)
    {
    int count=0;
    if(str1.length()!=str2.length())
    {
    return false;
    }
    else
    {
    for(int i=0;i<str1.length();i++)
    {
    count=count+str1.charAt(i);
    }
    for(int i=0;i<str2.length();i++)
    {
    count=count-str2.charAt(i);
    }
    }
    if(count==0)
    return true;
    return false;

    }
    public static void main(String[] args) {
    String s1="aaabbb";
    String s2="bbaaba";
    System.out.println(checkAnagram(s1, s2));
    }
    }