Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Anagram in C

Anagram:
The algorithm here we constructed to find the number of characters in the given 2 strings and to compare each and every character in both the strings to be same is called as anagram
Example:
if string1="forget" and string2="oretfg"
In the program the string length are same and each character in both the strings are present one time.so this strings are called anagram


#include <stdio.h>
 
int check_anagram(char [], char []);
 
int main()
{
   char aa[100], bb[100];
   int flagvar;
 
   printf("Enter first string\n");
   gets(aa);
 
   printf("Enter second string\n");
   gets(bb);
 
   flagvar = check_anagram(a, b);
 
   if (flagvar == 1)
      printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
   else
      printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);
 
   return 0;
}
 
int check_anagram(char a[], char b[])
{
   int first[26] = {0}, second[26] = {0}, c = 0;
 
   while (a[c] != '\0')
   {
      first[a[c]-'a']++;
      c++;
   }
 
   c = 0;
 
   while (b[c] != '\0')
   {
      second[b[c]-'a']++;
      c++;
   }
 
   for (c = 0; c < 26; c++)
   {
      if (first[c] != second[c])
         return 0;
   }
 
   return 1;
}