/* 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;
}

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.

Edited 7 Years Ago by Ancient Dragon: n/a

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

Edited 7 Years Ago by mvmalderen: n/a

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

>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).

Edited 7 Years Ago by mvmalderen: fix code indenting

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
*/
Comments
A little demostration goes a long way
Thanks for the demonstration :)
The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.