Created
March 19, 2014 08:21
-
-
Save threeifbywhiskey/9637523 to your computer and use it in GitHub Desktop.
This is a solution to /r/dailyprogrammer's Intermediate challenge #154, Gorellian Alphabet Sort.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MAXWORD 80 | |
char alphabet[27]; | |
int gorellian_strcmp(const void *va, const void *vb) | |
{ | |
size_t i; | |
char *a, *b; | |
a = strcpy(malloc(strlen(va)), *(char **) va); | |
b = strcpy(malloc(strlen(vb)), *(char **) vb); | |
for (i = 0; i < strlen(a); ++i) | |
a[i] = strchr(alphabet, a[i] | 32) - alphabet + 1; | |
for (i = 0; i < strlen(b); ++i) | |
b[i] = strchr(alphabet, b[i] | 32) - alphabet + 1; | |
return strcmp(a, b); | |
} | |
int main(void) | |
{ | |
int i, n; | |
char **words; | |
scanf("%d", &n); | |
scanf("%s", alphabet); | |
for (i = 0; i < 26; ++i) | |
alphabet[i] |= 32; /* Capitalize the alphabet. */ | |
words = calloc(n, sizeof(char *)); | |
for (i = 0; i < n; ++i) { | |
words[i] = malloc(MAXWORD); | |
scanf("%s ", words[i]); | |
} | |
qsort(words, n, sizeof(*words), gorellian_strcmp); | |
for (i = 0; i < n; ++i) | |
printf("%s\n", words[i]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment