Q. Write a program to check whether the two strings anagram of each other or not.



Anagram :- When two strings contain the same characters irrespective of the order of occurrence then such strings are known as Anagram.
For Example :-
String1 = LISTEN
String2 = SILENT
So as we can see that both strings have same characters, only the order of characters is different so this is an Anagram.

Anagram Algorithm

START
  Step->1 : Define two strings.
  Step->2 : Check for their lengths. If the lengths are not equal, then strings are not an anagram.
  Step->3 : Else, convert the string to lower case character to make the comparison easy.
  Step->4 : Some language allows the strings to provide inbuilt function for sorting of string. If not then convert them to character array for sorting.
  Step->5 : Sort the array.
  Step->6 : Finally, check for the equality of content.
STOP


Anagram Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    int check_anagram(char [], char []);
    
    int main()
    {
      char str1[1000]="listen", str2[1000]="silent";
    
      if (check_anagram(str1, str2))
        printf("The two strings are anagram of each other.\n");
      else
        printf("The two strings are not anagram of each other\n");
    
      return 0;
    }
    
    int check_anagram(char str1[], char str2[])
    {
      int first[26] = {0}, second[26] = {0}, third=0;
    
      // Calculating frequency of characters of the first string
    
      while (str1[third] != '\0') {
        first[str1[third]-'a']++;
        third++;
      }
    
      third = 0;
    
      while (str2[third] != '\0') {
        second[str2[third]-'a']++;
        third++;
      }
    
      // Comparing the frequency of characters
    
      for (third = 0; third < 26; third++)
        if (first[third] != second[third])
          return 0;
    
      return 1;
    }
    
    #include <bits/stdc++.h>
    
    using namespace std; 
      
    
    bool anagram(string str1, string str2) 
    { 
        // Get lengths of both strings 
        int len1 = str1.length(); 
        int len2 = str2.length(); 
      
        // If length of both strings is not same, then they 
        // cannot be anagram 
        if (len1 != len2) 
            return false; 
      
        // Sort both the strings 
        sort(str1.begin(), str1.end()); 
        sort(str2.begin(), str2.end()); 
      
        // Compare sorted strings 
        for (int i = 0; i < len1; i++) 
            if (str1[i] != str2[i]) 
                return false; 
      
        return true; 
    } 
      
    // Driver code 
    int main() 
    { 
        string str1 = "listen"; 
        string str2 = "silent"; 
        if (anagram(str1, str2)) 
            cout << "The two strings are anagram of each other"; 
        else
            cout << "The two strings are not anagram of each other"; 
      
        return 0; 
    } 
    
    import java.io.*; 
    import java.util.Arrays; 
    import java.util.Collections; 
      
    class LFC { 
      
    
        static boolean anagram(char[] str1, char[] str2) 
        { 
            // Get lenghts of both strings 
            int len1 = str1.length; 
            int len2 = str2.length; 
      
            // If length of both strings is not same, 
            // then they cannot be anagram 
            if (len1 != len2) 
                return false; 
      
            // Sort both strings 
            Arrays.sort(str1); 
            Arrays.sort(str2); 
      
            // Compare sorted strings 
            for (int i = 0; i < len1; 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[] = { 'l', 'i', 's', 't', 'e', 'n' }; 
            char str2[] = { 's', 'i', 'l', 'e', 'n', 't' }; 
            if (anagram(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"); 
        } 
    } 
      
    
    def anagram(str1, str2):  
        # Get lengths of both strings  
        len1 = len(str1)  
        len2 = len(str2)  
      
        # If lenght of both strings is not same, then  
        # they cannot be anagram  
        if len1 != len2:  
            return 0
      
        # Sort both strings  
        str1 = sorted(str1) 
        str2 = sorted(str2) 
      
        # Compare sorted strings  
        for i in range(0, len1):  
            if str1[i] != str2[i]:  
                return 0
      
        return 1
      
      
    # Driver program to test the above function  
    str1 = "listen"
    str2 = "silent"
    if anagram(str1, str2):  
        print ("The two strings are anagram of each other") 
    else:  
        print ("The two strings are not anagram of each other") 
      
    using System;
     
    public class LFC {
       public static void Main () {
          string str1 = "listen";
          string str2 =  "silent";
          char[] ch1 = str1.ToLower().ToCharArray();  
          char[] ch2 = str2.ToLower().ToCharArray();  
          Array.Sort(ch1);  
          Array.Sort(ch2);  
          string val1 = new string(ch1);  
          string val2 = new string(ch2);  
          
          if (val1 == val2) {  
             Console.WriteLine("The two strings are anagram of each other");  
          } else {  
             Console.WriteLine("The two strings are not anagram of each other");  
          }  
       }
    }
    
     function anagram($str1, $str2) 
        { 
            if (count_chars($str1, 1) == count_chars($str2, 1)) 
                return 'The two strings are anagram of each other'; 
            else 
                return 'The two strings are not anagram of each other';        
        } 
      
        // Driver code 
        print_r(anagram('listen', 'silent')."\n"); 
      
    
    #include 
    int check_anagram(char [], char []);
    
    int main()
    {
      char str1[1000]="listen", str2[1000]="silent";
    
      if (check_anagram(str1, str2))
        printf("The two strings are anagram of each other\n");
      else
        printf("The two strings are not anagram of each other\n");
    
      return 0;
    }
    
    int check_anagram(char str1[], char str2[])
    {
      int first[26] = {0}, second[26] = {0}, third=0;
    
      // Calculating frequency of characters of the first string
    
      while (str1[third] != '\0') {
        first[str1[third]-'a']++;
        third++;
      }
    
      third = 0;
    
      while (str2[third] != '\0') {
        second[str2[third]-'a']++;
        third++;
      }
    
      // Comparing the frequency of characters
    
      for (third = 0; third < 26; third++)
        if (first[third] != second[third])
          return 0;
    
      return 1;
    }
    

    Output

    The two strings are anagram of each other
    

    Recommended Programs

       Binary search algorithm & program
       Program to check armstrong number