944,059 Members | Top Members by Rank

Ad:
  • C Code Snippet
  • Views: 1196
  • C RSS
1

isAnagram() function in C

by on Oct 1st, 2009
As the title says: a C function for detecting anagrams.
Returns true if both strings are anagrams, returns false otherwise.
C Code Snippet (Toggle Plain Text)
  1. /* Check if s1 and s2 are anagrams of each other */
  2. int isAnagram(const char *s1, const char *s2)
  3. {
  4. int ha[CHAR_MAX] = {0};
  5. int i;
  6.  
  7. while(*s1 && *s2) {
  8. ha[*s1++]++;
  9. ha[*s2++]--;
  10. }
  11.  
  12. if(*s1 || *s2) return 0;
  13.  
  14. for(i=0; i<CHAR_MAX; ++i) {
  15. if(ha[i]) return 0;
  16. }
  17.  
  18. return 1;
  19. }
Comments on this Code Snippet
Oct 1st, 2009
-7

Re: isAnagram() function in C

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.
Last edited by Ancient Dragon; Oct 1st, 2009 at 1:09 pm.
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 1st, 2009
0

Re: isAnagram() function in C

Oops, I forgot to mention you should include the limits.h header.
Or... did I miss the point of your post?
Last edited by tux4life; Oct 1st, 2009 at 1:26 pm.
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Oct 1st, 2009
0

Re: isAnagram() function in C

It might be nice to provide some "test" or "driver" code in which some example of this function's input and output are demonstrated.
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 1st, 2009
0

Re: isAnagram() function in C

>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:
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. int isAnagram(const char *s1, const char *s2);
  5.  
  6. int main(void)
  7. {
  8. char str1[80];
  9. char str2[80];
  10.  
  11. for(;;)
  12. {
  13. printf("\nFirst string: ");
  14. fgets(str1, 80, stdin);
  15.  
  16. printf("Enter second string: ");
  17. fgets(str2, 80, stdin);
  18.  
  19. if( isAnagram(str1, str2) )
  20. printf("Both strings are anagrams.\n");
  21. else
  22. printf("Both strings are NOT anagrams.\n");
  23. }
  24.  
  25. return 0;
  26. }
  27.  
  28. /* Check if s1 and s2 are anagrams of each other */
  29. int isAnagram(const char *s1, const char *s2)
  30. {
  31. int ha[CHAR_MAX] = {0};
  32. int i;
  33.  
  34. while(*s1 && *s2) {
  35. ha[*s1++]++;
  36. ha[*s2++]--;
  37. }
  38.  
  39. if(*s1 || *s2) return 0;
  40.  
  41. for(i=0; i<CHAR_MAX; ++i) {
  42. if(ha[i]) return 0;
  43. }
  44.  
  45. return 1;
  46. }
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...).
Last edited by tux4life; Oct 1st, 2009 at 2:21 pm. Reason: fix code indenting
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Oct 1st, 2009
3

Re: isAnagram() function in C

Danke. I was thinking something a little more canned:
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. /* Check if s1 and s2 are anagrams of each other */
  5. int isAnagram(const char *s1, const char *s2)
  6. {
  7. int ha[CHAR_MAX] = {0};
  8. int i;
  9.  
  10. while ( *s1 && *s2 )
  11. {
  12. ha[*s1++]++;
  13. ha[*s2++]--;
  14. }
  15.  
  16. if ( *s1 || *s2 )
  17. {
  18. return 0;
  19. }
  20.  
  21. for ( i = 0; i < CHAR_MAX; ++i )
  22. {
  23. if ( ha[i] )
  24. {
  25. return 0;
  26. }
  27. }
  28.  
  29. return 1;
  30. }
  31.  
  32. #if defined TEST_DRIVER && TEST_DRIVER > 0
  33.  
  34. void anagram_test(const char *s1, const char *s2)
  35. {
  36. printf("isAnagram(\"%s\",\"%s\") = %d\n", s1, s2, isAnagram(s1, s2));
  37. }
  38.  
  39. int main(void)
  40. {
  41. /** http://en.wikipedia.org/wiki/Anagram */
  42. anagram_test("orchestra", "carthorse");
  43. anagram_test("A decimal point", "I'm a dot in place");
  44. return 0;
  45. }
  46.  
  47. #endif
  48.  
  49. /* my output
  50. isAnagram("orchestra","carthorse") = 1
  51. isAnagram("A decimal point","I'm a dot in place") = 0
  52. */
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Message:
Previous Thread in C Forum Timeline: gtk runtime nessary?
Next Thread in C Forum Timeline: count number of characters program in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC