Problem Description: King Alexander wants every chariot to line up for the start of her Winter-eve ball. He has asked you, Twilight Sparkle, to sort the horse chariots alphabetically but with royalty in front. Royal horses chariot have diamonds in their names. Constraints: 1 <= Names <= 100 Input Format: list of horse chariots numbering anywhere from 1 to 40 horse chariot, with 1 per line. The end of input will be marked with END on a line by itself. Names should be no longer than 100 characters in length, and to only contain letters and spaces. Output Format: Print the output in a separate lines contains Sort and list the horse chariots alphabetically in ascending order ("A" "first"), ignoring case. However, any horse chariot with a diamond in their name must be placed at the "top" of the list (before the "A's" start) in the diamond order given in the Discussion section below.
C Code


#include <stdio.h>

#include <string.h>

#include <ctype.h>

#define MAXP 40

#define BUFLEN 101

/* ===== Mandatory Keyword 1 ===== */

char *gems[]={"NONE", "Garnet", "Amethyst", "Aquamarine", "Diamond",

              "Emerald", "Pearl", "Ruby", "Peridot",

              "Sapphire", "Tourmaline", "Topaz", "Lapis"};

int priority(char name[])

{

    int best=0;

    char temp[BUFLEN], word[BUFLEN];

    strcpy(temp,name);

    for(int i=0;temp[i];i++)

        temp[i]=tolower(temp[i]);

    char *token=strtok(temp," ");

    while(token)

    {

        for(int i=1;i<13;i++)

        {

            strcpy(word,gems[i]);

            for(int j=0;word[j];j++)

                word[j]=tolower(word[j]);

            if(strcmp(token,word)==0 && i>best)

                best=i;

        }

        token=strtok(NULL," ");

    }

    return best;

}

int main()

{

    /* ===== Mandatory Keyword 2 ===== */

    char ponies[MAXP][BUFLEN];

    int pr[MAXP],n=0;

    while(fgets(ponies[n],BUFLEN,stdin))

    {

        ponies[n][strcspn(ponies[n],"\n")]=0;

        if(strcmp(ponies[n],"END")==0)

            break;

        pr[n]=priority(ponies[n]);

        n++;

    }

    for(int a=0;a<n-1;a++)

        for(int b=0;b<n-a-1;b++)

            if(pr[b]<pr[b+1] ||

              (pr[b]==pr[b+1] &&

              /* ===== Mandatory Keyword 3 ===== */

              strcmp(ponies[b], ponies[b+1])>0))

            {

                char t[BUFLEN];

                int tp;

                strcpy(t,ponies[b]);

                strcpy(ponies[b],ponies[b+1]);

                strcpy(ponies[b+1],t);

                tp=pr[b];

                pr[b]=pr[b+1];

                pr[b+1]=tp;

            }

    for(int i=0;i<n;i++)

        printf("%s\n",ponies[i]);

    return 0;

}