/*--------------------------------------*/
/* ELEC 2220 Homework 2                 */
/* Read five names from the keyboard	*/
/* Print in alphabetical order		*/
/*--------------------------------------*/

#include "stdio.h"
#include "string.h"

void main ()
{
	char name_ary[5][10];	//array of 5, 10-char names
	char temp[10];		//array for one name
	int i,j;		//temp integers
	
	/* Get the names from the keyboard */
	puts ("Enter names:");
	for (i=0; i<5; i++) {
		gets(name_ary[i]);	//get one name into array
		/* Convert to one upper-case + rest lower-case */
	}

	/* Sort the list (bubble sort) */
	for (i=0; i<4; i++) //outer loop - 4 passes through list
	{
	  for (j=0; j < 4-i; j++)  //inner loop - compare pairs
	    {
		  /* Compare adjacent strings */
	      if (strcmp(name_ary[j],name_ary[j+1]) > 0)
	        {
			 /* Swap if first > second */
	         strcpy (temp,name_ary[j]);
	         strcpy (name_ary[j],name_ary[j+1]);
	         strcpy (name_ary[j+1],temp);
	        }
	     }
	}

	/* Print the sorted list */	     
	puts ("Sorted list:");
	for (i=0; i<5; i++)
	   printf("%s\n",name_ary[i]);
}
