/*
 * Copyright (C) Linux-Manipur 2005-2008 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This program sorts a given list of string in accordance with the given 
 * sequence of letters.
 */


#include <stdio.h>
#include <string.h>


int get_pos_count(char c){

	char sequence[]={'\0', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x','c','v', 'b', 'n', 'm'};
	int i;
	for (i =0; i < 26; i++) {
		if (sequence[i]== c)
			return i;
	}
	return -1;
}

/*
 * Implementation of strcmp according to the given sequence of letters.
 */

int mystrcmp(char *a, char *b) {

	//GO TO THE FIRST UNEQUAL CHARACTERS
	while (*a && (get_pos_count (*a) == get_pos_count(*b))){
		a++;
		b++;
	}
	;
	return  (get_pos_count (*a) - get_pos_count(*b));

}
 
int main ()
{
	int n,i, j;
	char names[20][50], temp[50];

	//GET THE INPUTS
	printf ("Enter the number of strings:");
	scanf ("%d", &n);
	for (i=0; i<n; i++){
		scanf ("%s", names[i]);
	}

	//SELECCTION SORT
	for (i =0; i< n; i++) {
		for (j = i+1; j <n; j++) {
			if (mystrcmp (names[i], names[j]) > 0)
			{
				strcpy (temp, names[i]);
				strcpy (names[i], names[j]);
				strcpy (names[j], temp);
			}
		}
	}

	printf ("\nThe sorted list is :-\n");	
	for (i=0; i<n; i++){
		printf ("%s\n", names[i]);
	}
	return 1;
}



