C Programming Code Examples C > Strings Code Examples C Program to Check whether two Strings are Anagrams C Program to Check whether two Strings are Anagrams This program takes two strings as input and checks whether two strings are anagrams. - Take two strings as input and store them in the arrays array1[] and array2[] respectively. - In the function find_anagram() using while statement sort both the arrays. After sorting compare them using for loop. - If all the strings are equal then the two strings are anagrams, otherwise they are not anagrams. #include <stdio.h> int find_anagram(char [], char []); int main() { char array1[100], array2[100]; int flag; printf("Enter the string\n"); gets(array1); printf("Enter another string\n"); gets(array2); flag = find_anagram(array1, array2); if (flag == 1) printf(""%s" and "%s" are anagrams.\n", array1, array2); else printf(""%s" and "%s" are not anagrams.\n", array1, array2); return 0; } int find_anagram(char array1[], char array2[]) { int num1[26] = {0}, num2[26] = {0}, j = 0; while (array1[j] != '\0') { num1[array1[j] - 'a']++; j++; } j = 0; while (array2[j] != '\0') { num2[array2[j] -'a']++; j++; } for (j = 0; j < 26; j++) { if (num1[j] != num2[j]) return 0; } return 1; }