/* hw5p2c.c */

#include <stdio.h>
#include "genlib.h"

#define MAXWORDLEN 20

void GetWord (char word[]);
bool LexOrder (char wordA[], char wordB[]);

void main()
{
  char wordA[MAXWORDLEN];
  char wordB[MAXWORDLEN];
  GetWord(wordA);
  GetWord(wordB);
  if (LexOrder(wordA, wordB))
    printf("%s comes before %s\n", wordA, wordB);
  else
    printf("%s comes before %s\n", wordB, wordA);
}

void GetWord (char word[])
{
  int i;
  printf("enter a word: ");
  for (i = 0; (word[i] = getchar()) != '\n'; i++); /* getchar until newline */
  word[i] = '\0';   /* put null character at end or word */
}

bool LexOrder (char wordA[], char wordB[])
{
  int i;
  char a,b;
  for (i = 0; (a = wordA[i]) != '\0'; i++) { /* compare each char of wordA to wordB */
    if ((b = wordB[i]) == '\0')/* end of wordB reached meaning wordB */
      return FALSE;	       /* is a substring or wordA */
    if (a < b)
      return TRUE;             /* wordA comes before word B */
    if (b < a)
      return FALSE;            /* wordB comes before word A */
  }                            /* otherwise move on to compare the next char */
  return TRUE; /* end of wordA reached meaning wordA is a substring of wordB */
}
