Computer Science, asked by 2110, 1 year ago

icse class 10 students plzz answer this
write a program to check two words are in anagram
(anagram is set of two words where they have same number of letters and even same letters in interchanged order)
eg: WOLF
FLOW here there are same 4 letters and even the letters are same


2110: dr friend i don't even want the full prg but at least the process
2110: yes tq pls answer as soon as possible
msndipapal: i have already answered
msndipapal: the same thing but it has more comments

Answers

Answered by msndipapal
1

// JAVA program to check whether two strings  

// are anagrams of each other  

import java.io.*;  

import java.util.*;    

class GFG {  

 

   /* function to check whether two strings are  

   anagram of each other */

   static boolean areAnagram(char[] str1, char[] str2)  

   {  

       // Get lenghts of both strings  

       int n1 = str1.length;  

       int n2 = str2.length;  

 

       // If length of both strings is not same,  

       // then they cannot be anagram  

       if (n1 != n2)  

           return false;  

 

       // Sort both strings  

       Arrays.sort(str1);  

       Arrays.sort(str2);  

 

       // Compare sorted strings  

       for (int i = 0; i < n1; i++)  

           if (str1[i] != str2[i])  

               return false;  

 

       return true;  

   }  

 

   /* Driver program to test to print printDups*/

   public static void main(String args[])  

   {  

       char str1[] = { 't', 'e', 's', 't' };  

       char str2[] = { 't', 't', 'e', 'w' };  

       if (areAnagram(str1, str2))  

           System.out.println("The two strings are"

                              + " anagram of each other");  

       else

           System.out.println("The two strings are not"

                              + " anagram of each other");  

   }  

}  

Similar questions