| | |
isAnagram() function in C
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
As the title says: a C function for detecting anagrams.
Returns true if both strings are anagrams, returns false otherwise.
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; }
-7
•
•
•
•
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.
0
•
•
•
•
Oops, I forgot to mention you should include the limits.h header.
Or... did I miss the point of your post?
Or... did I miss the point of your post?
Last edited by tux4life; Oct 1st, 2009 at 1:26 pm.
0
•
•
•
•
It might be nice to provide some "test" or "driver" code in which some example of this function's input and output are demonstrated.
0
•
•
•
•
>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:
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...
).
Okay, here you go:
C Syntax (Toggle Plain Text)
#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; }
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
3
•
•
•
•
Danke. I was thinking something a little more canned:
C Syntax (Toggle Plain Text)
#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 */
Similar Threads
- Newbie function question (Python)
- My python program/function! (Python)
- Anagram Function - Reapeated letters Problem (C++)
- Function that returns void (C++)
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi



