954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

isAnagram() function in C

By Mathias Van Malderen on Oct 1st, 2009 9:50 pm

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

/* 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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You