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
• 418
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)

OR
OR
Register

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

• Hire
• Post Projects

### Post Projects

• All at 0 Cost ....
• Post Tech Job
• Select Best Bidder
• Track the Project
• Approve Work and Pay safely
• Browse Nerds
• Work
• Find Projects Find Projects
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Manage
• Company Company

### Manage Company

• All at 0 Cost ....
• Manage Company and Employee Profiles
• Company wide Employee Productivity Reports
• Knowledge Sharing and Collaboration Tools
• Get Sales Lead and Bid for Tech Projects
• Send Invoices and Receive Payment Safely
• Learn
• Nerd Digest Nerd Digest
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Tech Q & A Tech Q & A
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General