/* Anagramski, a tiny tool that looks for all anagrams of a word in a given file and prints all found ones to stdout. See http://www.use-strict.de/software/anagramski/ for details. Compileable i.e. with: gcc -Wall -ansi -pedantic -o anagramski anagramski.c Copyright (c) 2007 Alex Linke . All right reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. $_Id: anagramski.c,v 1.1 2007-06-12 13:47:49 alex Exp $ */ #include #include /* exit */ #include /* tolower */ #include /* strcmp, strlen */ #if !defined(WIN32) #include /* strcasecmp */ #define OPT_INDICATOR '-' #else #define OPT_INDICATOR '/' #endif #if defined(__FreeBSD__) #include /* getopt */ #else /* __CYGWIN__, __linux__, WIN32 */ #include #endif /* Alphabet: ASCII lowercase letters */ #define ALPHABET 26 #define TRUE 0 #define FALSE -1 typedef unsigned short int usint; void error (char *); void help (char *, usint); void cha_bld (char *, usint []); usint cha_eql (usint [], usint [] ); int main (int argc, char *argv[]) { FILE *fp; char buf[100], ch, *word, *file = NULL; /* Example on character array representation of a word: "Alex" element #: 12345678901234567890123456 value: 10001000000100000000000100 */ usint wa[ALPHABET], ta[ALPHABET]; /* Parse commandline options */ while ((ch = getopt(argc, argv, "w:f:h")) != FALSE) { switch(ch) { case 'w': word = optarg; break; case 'f': file = optarg; break; case 'h': help(argv[0], 0); break; default: help(argv[0], 1); } } /* Are word and file defined? */ if (word == NULL || file == NULL) help(argv[0], 1); /* Open file read-only */ if ((fp = fopen(file, "r")) == NULL) error("Failed to open file"); /* Build character representation of the word to search anagrams for */ cha_bld(word, wa); /* Read the input file word by word and look for anagrams */ while (fscanf(fp, "%s", buf) != EOF) { cha_bld(buf, ta); #if defined(WIN32) if (cha_eql(wa, ta) == TRUE && strcmp(word, buf) != 0) #else if (cha_eql(wa, ta) == TRUE && strcasecmp(word, buf) != 0) #endif puts(buf); } fclose(fp); return 0; } /* Build array from character count representation of a given word */ void cha_bld (char *w, usint a[ALPHABET]) { usint len = strlen(w); usint i, val = 0; /* Initialize array elements to 0 */ memset(a, 0, ALPHABET * sizeof(usint)); for (i=0; i < len; i++) { /* Lowercase the char, substract 97 (Mnemonic: ASCII 'a')... */ val = (usint) (tolower ((int) w[i]) - 97); /* ...skip out-of-set chars and increment character counter */ if (val <= ALPHABET) /* val is an usint and therefore always > 0 */ a[val]++; } } /* Check if two character representation arrays are equal */ usint cha_eql (usint a[ALPHABET], usint b[ALPHABET] ) { usint i=0; for (i=0; i < ALPHABET; i++) if (a[i] != b[i]) return FALSE; return TRUE; } void error (char *msg) { fprintf(stderr, "ERROR: %s\n", msg); exit(1); } void help (char *prg, usint retval) { printf("usage: %s %cw WORD %cf FILE\n", prg, OPT_INDICATOR, OPT_INDICATOR); exit(retval); } /* vim: sts=2 enc=utf-8 */