Saturday, 25 January 2020

/*Program to check whether two strings are Anagram or Not*/

Anagram Definition:

An anagram of a string is another string that contains same characters, only the order of characters can be different

Example: Weak, Wake
                 Mad, Dam

/* Program using Java*/

class StringAnagram
{
public static void main(String[] a) throws Exception
{
boolean flag=true;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("enter two strings");
str1=br.readLine();
str2=br.readLine();  
 int n1 = str1.length;
 int n2 = str2.length;
        if (n1 != n2)
            return false;
        char[] ch1 = new char[str1.length()];
        for (int i = 0; i < str1.length(); i++) {
            ch1[i] = str1.charAt(i);
        char[] ch2 = new char[str2.length()];
        for (int i = 0; i < str1.length(); i++) {
            ch2[i] = str1.charAt(i);
      
        Arrays.sort(str1);
        Arrays.sort(str2);
    
        for (int i = 0; i < n1; i++)
            if (str1[i] != str2[i])
               flag=false;
        if(flag==true)
                System.out.println("Strings are Anagram");
       else
                System.out.println("Strings are not Anagram");
 }
}

No comments:

Post a Comment