From demon!bnr.co.uk!zaphod.axion.bt.co.uk!uknet!pipex!uunet!haven.umd.edu!darwin.sura.net!udel!rochester!rocksanne!octopus!dave Sun Apr 11 21:34:18 GMT 1993
Article: 1005 of alt.sources
Newsgroups: alt.sources
Path: demon!bnr.co.uk!zaphod.axion.bt.co.uk!uknet!pipex!uunet!haven.umd.edu!darwin.sura.net!udel!rochester!rocksanne!octopus!dave
From: dave@octopus (Dave Redford)
Subject: Simple/quick anagram program
Message-ID: <1993Apr9.194516.13635@spectrum.xerox.com>
Sender: news@spectrum.xerox.com
Organization: Xerox
X-Newsreader: TIN [version 1.1 PL9]
Date: Fri, 9 Apr 1993 19:45:16 GMT
Lines: 126

Here's a simple anagram program. It only finds valid/real words which match all
the letters in the supplied word. The user can provide his own list of not-so-
real words if he wants.....


----------------------------------

/* anag.c - D.K.Redford 9 April 93.
 *
 * Simple anagram finder. 
 *
 * Uses Unix System and User supplied Dictionaries...
 *	(format = simple list of words seperated by new line).
 *
 * Sorts target word into alpha order then compares this sorted word with
 * all words in the two Dictiionaries similarly sorted.
 * Prints out any matches.
 */

#include <stdio.h>
#include <stdlib.h>

#define SYSTEM	"/usr/dict/words"	/* supply Unix Dictionary path here */
#define USER	"words"			/* and yours here */

char *prog;

static void error();
static void scan();
static void sort();

/*----------------------------------------------------------------------*/
int main(argc, argv)
int argc;
char **argv;
{
  FILE *fpi;
  char usr_word[300];

  prog = argv[0];

  if (argc != 2)
  {
    printf("Usage: %s word\n", prog);
    exit(1);
  }

  sort(argv[1], usr_word);

  if ((fpi = fopen(SYSTEM, "r")) == NULL)
    error("Can't open system Dictionary");

  scan(fpi, usr_word);

  if ((fpi = fopen(USER, "r")) == NULL)
    error("Can't open user Dictionary");

  scan(fpi, usr_word);

  exit(0);
}

/*----------------------------------------------------------------------*/
static void error(s)
char *s;
{
  printf("%s: %s\n", prog, s);
  exit(1);
}

/*----------------------------------------------------------------------*/
static void scan(fp, usr_word)
FILE *fp;
char *usr_word;
{
  char buf[50], new_word[50];

  while(fgets(buf, 50, fp) != NULL)
  {
    sort(buf, new_word);

    if (strcasecmp(usr_word, new_word) == 0)
      printf("%s\n", buf);
  }
  (void)fclose(fp);
}

/*----------------------------------------------------------------------*/
static void sort(in, out)
char *in, *out;
{
  char tmp;
  int i,l;

  l = strlen(in);

  if (in[l-1] == '\n')	/* throw away new lines */
  {
    in[l-1] = '\0';
    l--;
  }

  strcpy(out, in);

  if (l>1)
  {
    do			/* simple bubble sort */
    {
      tmp = '\0';

      for (i=0; i<l-2; i++)
      {
	if (out[i+1] < out[i])
	{
	  tmp       = out[i];
	  out[i]    = out[i+1];
	  out[i+1]  = tmp;
	}
      }
    } while (tmp != '\0');
  }
}

/* ends */




