/* Problem 4--True Alphabeting Sorting
   This problem is particularly easy in C; you can use the built-in
   qsort function to avoid writing your own specific sort. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXL 10

int main (int argc, char **argv);
int ordercmp (const void *a, const void *b);

FILE *in, *out;

int main (int argc, char **argv) {

  char A[1+MAXL];

 in = fopen ("prob4.in","r");
 out = fopen ("prob4.out","w");
 while (1) {
  fscanf (in,"%s",A); /* read in a word */
  if (strcmp (A,"END")==0) break;
  qsort (A,strlen (A),1,ordercmp); /* sort it according to our order */
  fprintf (out,"%s\n",A);
 }
 fclose (in);
 fclose (out);
 return EXIT_SUCCESS;
}

/* ordercmp determines the ordering for our letters:  A becomes 0; a
   becomes 1; B becomes 2; b becomes 3, and so forth. */

int ordercmp (const void *a, const void *b) {

 char aa = *(char *)a, bb = *(char *)b;
 int ai = aa < 'a' ? 2*(aa-'A') : 2*(aa-'a')+1,
     bi = bb < 'a' ? 2*(bb-'A') : 2*(bb-'a')+1;
 return ai - bi;
}
