/*-----------------------------------------------------------------------------
Filename   : anagram.c
Name       : Tristan Miller
SID#       : 123456789
Description: function for testing for anagrams
-----------------------------------------------------------------------------*/

#define MAX_ASCII 127
#define FALSE 0
#define TRUE 1

/*-----------------------------------------------------------------------------
Name   : anagram()
Purpose: determines whether given strings s1 and s2 are anagrams
Returns: TRUE if s1 and s2 are anagrams; else FALSE
-----------------------------------------------------------------------------*/
int anagram(const char *s1, const char *s2) {
  /* Declare a frequency table for each ASCII character */
  int freq[MAX_ASCII + 1], i;

  /* Initialize freq. table to 0 */
  for (i = 0; i <= MAX_ASCII; i++)
    freq[i] = 0;

  /* Fill in freq. table for s1 */
  while (*s1)
    freq[(int)*s1++]++;

  /* Fill in negative freq. table for s2 */
  while (*s2)
    freq[(int)*s2++]--;

  /* If s1 and s2 are anagrams, all cells should be 0 */
  for (i = 0 ; i <= MAX_ASCII; i++)
    if (freq[i])
      return FALSE;

  return TRUE;
}




/*=============================================================================
Here's a short program which tests our anagram() function.
(This didn't have to be submitted with the file but is here to
illustrate the type of input that needs to be checked.)
=============================================================================*/
#include <stdio.h>
#include <stdlib.h>

int anagram(const char *, const char *);

int main(void) {
  char *a[] = {"d", "do", "dog", "c", "co", "cat"};
  char *b[] = {"d", "od", "odg", "i", "cto", "bat"};
  int i;

  for (i = 0; i < 6; i++)
    printf("%s = %s ? %s\n", a[i], b[i], anagram(a[i], b[i]) ? "yes" : "no");

  return EXIT_SUCCESS;
}

