isAnagram() function in C

mvmalderen

As the title says: a C function for detecting anagrams.
Returns true if both strings are anagrams, returns false otherwise.

Ancient Dragon commented: Very good, and a unique way of doing it :) +36
671 Views
About the Author

Just check out [my thread](http://www.daniweb.com/community-center/community-introductions/threads/205430/hello-from-tux) in the community introductions if you want to know more about me.

/* Check if s1 and s2 are anagrams of each other */
int isAnagram(const char *s1, const char *s2)
{
  int ha[CHAR_MAX] = {0};
  int i;

  while(*s1 && *s2) {
    ha[*s1++]++;
    ha[*s2++]--;
  }

  if(*s1 || *s2) return 0;

  for(i=0; i<CHAR_MAX; ++i) {
    if(ha[i]) return 0;
  }

  return 1;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

CHAR_MAX is undefined -- and should be declared as const int CHAR_MAX = 255; Anything smaller than 255 and that function might scribble outside the bounds of the array.

mvmalderen 2,072 Postaholic

Oops, I forgot to mention you should include the limits.h header.
Or... did I miss the point of your post?

Dave Sinkula 2,398 long time no c Team Colleague

It might be nice to provide some "test" or "driver" code in which some example of this function's input and output are demonstrated.

mvmalderen 2,072 Postaholic

>It might be nice to provide some "test" or "driver" code in which some example of this function's input and output are demonstrated.

Okay, here you go:

#include <stdio.h>
#include <limits.h>

int isAnagram(const char *s1, const char *s2);

int main(void)
{
    char str1[80];
    char str2[80];
	
    for(;;)
    {
        printf("\nFirst string: ");
        fgets(str1, 80, stdin);

        printf("Enter second string: ");
        fgets(str2, 80, stdin);

        if( isAnagram(str1, str2) )
            printf("Both strings are anagrams.\n");
        else
            printf("Both strings are NOT anagrams.\n");
    }

    return 0;
}

/* Check if s1 and s2 are anagrams of each other */
int isAnagram(const char *s1, const char *s2)
{
  int ha[CHAR_MAX] = {0};
  int i;

  while(*s1 && *s2) {
    ha[*s1++]++;
    ha[*s2++]--;
  }

  if(*s1 || *s2) return 0;

  for(i=0; i<CHAR_MAX; ++i) {
    if(ha[i]) return 0;
  }

  return 1;
}

And yes, I know there's no proper way to exit this program.
I just put in an infinite loop as a convenience to the user who wants to test the program without relaunching it every time he wants to test another couple of words (an ending condition for the loop can always be implemented though).

Another (more useful) application of this isAnagram() function would be if it's used in such a thing like a word descrambler, which can help you to cheat in solving scrabble puzzles (I know: the fun disappears then but yeh...:P).

Dave Sinkula 2,398 long time no c Team Colleague

Danke. I was thinking something a little more canned:

#include <stdio.h>
#include <limits.h>

/* Check if s1 and s2 are anagrams of each other */
int isAnagram(const char *s1, const char *s2)
{
   int ha[CHAR_MAX] = {0};
   int i;

   while ( *s1 && *s2 )
   {
      ha[*s1++]++;
      ha[*s2++]--;
   }

   if ( *s1 || *s2 )
   {
      return 0;
   }

   for ( i = 0; i < CHAR_MAX; ++i )
   {
      if ( ha[i] )
      {
         return 0;
      }
   }

   return 1;
}

#if defined TEST_DRIVER && TEST_DRIVER > 0

void anagram_test(const char *s1, const char *s2)
{
   printf("isAnagram(\"%s\",\"%s\") = %d\n", s1, s2, isAnagram(s1, s2));
}

int main(void)
{
   /** http://en.wikipedia.org/wiki/Anagram */
   anagram_test("orchestra", "carthorse");
   anagram_test("A decimal point", "I'm a dot in place");
   return 0;
}

#endif

/* my output
isAnagram("orchestra","carthorse") = 1
isAnagram("A decimal point","I'm a dot in place") = 0
*/
mvmalderen commented: Thanks for the demonstration :) +24
Aia commented: A little demostration goes a long way +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.