fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. enum /*untagged*/ { AbeforeB = -1, AequalsB = 0, AafterB = 1 };
  5.  
  6. int tailored_strcmp(const char *a, const char *b) {
  7. static char baseorder[] = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
  8. //if a or b is the empty string
  9. if (*a == 0) return AbeforeB;
  10. if (*b == 0) return AafterB;
  11. int lena = strlen(a);
  12. int lenb = strlen(b);
  13. char *pa = strchr(baseorder, *a);
  14. char *pb = strchr(baseorder, *b);
  15. if (pa == NULL) return lena < lenb ? AbeforeB : AafterB;
  16. if (pb == NULL) return lena < lenb ? AbeforeB : AafterB;
  17. if (pa == pb) {
  18. //need to check second letter
  19. if (a[1] == 0) return AbeforeB;
  20. if (b[1] == 0) return AafterB;
  21. char *ppa = strchr(baseorder, a[1]);
  22. char *ppb = strchr(baseorder, b[1]);
  23. if (ppa == NULL) return lena < lenb ? AbeforeB : AafterB;
  24. if (ppb == NULL) return lena < lenb ? AbeforeB : AafterB;
  25. if (ppa == ppb) return lena < lenb ? AbeforeB : AafterB;
  26. return ppa < ppb ? AbeforeB : AafterB;
  27. }
  28. return pa < pb ? AbeforeB : AafterB;
  29. }
  30.  
  31.  
  32. int main(void) {
  33. int i = 0, j = 0, count;
  34. char str[25][25], temp[25];
  35. while (1) {
  36. gets(str[i]);
  37. if (str[i][0] == '0') break;
  38. i++;
  39. }
  40. count = i;
  41. for (i = 0; i < count; i++) {
  42. for (j = i + 1; j < count; j++) {
  43. if (tailored_strcmp(str[i], str[j]) > 0) {
  44. strcpy(temp, str[i]);
  45. strcpy(str[i], str[j]);
  46. strcpy(str[j], temp);
  47. }
  48. }
  49. }
  50. for (i = 0; i < count; i++) {
  51. printf("[%s] ", str[i]);
  52. }
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 4504KB
stdin
Ggxx
Ggdddddd
GGGGG
ggggg

empty string above
aaa
aaaaaa
aaaaa
aaaa
aaa
aa
a
AAAAAAAAAAAAAAA
r
F
okz
oka
ok
okb
0
stdout
[] [AAAAAAAAAAAAAAA] [a] [aa] [aaa] [aaa] [aaaa] [aaaaa] [aaaaaa] [empty string above] [F] [GGGGG] [Ggxx] [Ggdddddd] [ggggg] [ok] [okb] [oka] [okz] [r]