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;
}
0 Comments