C Program to Check whether two Strings are Anagrams

«
»

This is a C Program to check whether two strings are anagrams.

Problem Description

This program takes two strings as input and checks whether two strings are anagrams.

Problem Solution

1. Take two strings as input.
2. Sort two strings according to their ASCII values.
3. Now compare the two strings. If both are equal, then they are anagrams. Otherwise they are not anagrams.

advertisement
Program/Source Code

Here is source code of the C Program to check whether two strings are anagrams. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1.  
  2. /*
  3.  * C Program to Check whether two Strings are Anagrams
  4.  */
  5. #include <stdio.h>
  6.  
  7. int find_anagram(char [], char []);
  8.  
  9. int main()
  10. {
  11.     char array1[100], array2[100];
  12.     int flag;
  13.  
  14.     printf("Enter the string\n");
  15.     gets(array1);
  16.     printf("Enter another string\n");
  17.     gets(array2);
  18.     flag = find_anagram(array1, array2);
  19.     if (flag == 1)
  20.         printf(""%s" and "%s" are anagrams.\n", array1, array2);
  21.     else
  22.         printf(""%s" and "%s" are not anagrams.\n", array1, array2);
  23.     return 0;
  24. }
  25.  
  26. int find_anagram(char array1[], char array2[])
  27. {
  28.     int num1[26] = {0}, num2[26] = {0}, i = 0;
  29.  
  30.     while (array1[i] != '\0')
  31.     {
  32.         num1[array1[i] - 'a']++;
  33.         i++;
  34.     }
  35.     i = 0;
  36.     while (array2[i] != '\0')
  37.     {
  38.         num2[array2[i] -'a']++;
  39.         i++;
  40.     }
  41.     for (i = 0; i < 26; i++)
  42.     {
  43.         if (num1[i] != num2[i])
  44.             return 0;
  45.     }
  46.     return 1;
  47. }
Program Explanation

1. Take two strings as input and store them in the arrays array1[] and array2[] respectively.
2. In the function find_anagram() using while statement sort both the arrays. After sorting compare them using for loop.
3. If all the strings are equal then the two strings are anagrams, otherwise they are not anagrams.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
Runtime Test Cases
 
Enter the string
abll
Enter another string
ball
"abll" and "ball" are anagrams.
 
 
Enter the string
tall
Enter another string
all
"tall" and "all" are not anagrams.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Take C Programming Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
advertisement
If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.