Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Check whether two strings are anagram of each other in C?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 379
    Comment on it
    // C program to check whether two strings are anagrams
    
    #include <stdio.h>
    #include <string.h>
    
    
    void quickSort(char *arr, int si, int ei); //Function declared for sorting a string
     
    bool areAnagram(char *str1, char *str2) //Function checking whether two strings are anagrams 
    {
        int n1 = strlen(str1);   //Evaluating length of first string
        int n2 = strlen(str2);   //Evaluating length of second string
     
        if (n1 != n2)     //checking two strings are of equal length
          return false;   
     
    //if two strings of equal length then sort them using quicksort
        quickSort(str1, 0, n1 - 1);  
        quickSort(str2, 0, n2 - 1);
     
        for (int i = 0; i < n1;  i++)    //// Compare sorted strings
           if (str1[i] != str2[i])
             return false;
     
        return true;
    }
     
    void exchange(char *a, char *b)    //function (exchange are needed for quickSort)
    {
        char temp;
        temp = *a;
        *a   = *b;
        *b   = temp;
    }
     
    int partition(char A[], int si, int ei)  //function (partition are needed for quickSort)
    {
        char x = A[ei];
        int i = (si - 1);
        int j;
     
        for (j = si; j <= ei - 1; j++)
        {
            if(A[j] <= x)
            {
                i++;
                exchange(&A[i], &A[j]);
            }
        }
        exchange (&A[i + 1], &A[ei]);
        return (i + 1);
    }
     
    void quickSort(char A[], int si, int ei)
    {
        int pi;    // Partitioning index 
        if(si < ei)
        {
            pi = partition(A, si, ei);
            quickSort(A, si, pi - 1);
            quickSort(A, pi + 1, ei);
        }
    }
     
    int main()
    {
        char str1[] = "demo";
        char str2[] = "emdo";
        if (areAnagram(str1, str2))
          printf("The two strings are anagram of each other");
        else
          printf("The two strings are not anagram of each other");
     
        return 0;
    }

    Output:

    The two strings are anagram of each other

    Explanation of above program:

    An anagram of a string can be defined as another string that contains same characters, only ordering of characters can be different. In above program, method used is to sort both strings and then compare their characters. In areAnagram() function first length of two strings are evaluated and compared as a a string can be anagram of other string if their length are equal. If length of strings are equal then those strings are sorted using quicksort, so that characters within strings can be in proper alphabetical order. After sorting, simply their characters are compared to determine they are anagram of each other and thus return true else returning false from  areAnagram() function.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: