fork(1) 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. char *ppa = strchr(baseorder, a[1]);
  20. char *ppb = strchr(baseorder, b[1]);
  21. if (ppa == NULL) return lena < lenb ? AbeforeB : AafterB;
  22. if (ppb == NULL) return lena < lenb ? AbeforeB : AafterB;
  23. if (ppa == ppb) return lena < lenb ? AbeforeB : AafterB;
  24. return ppa < ppb ? AbeforeB : AafterB;
  25. }
  26. return pa < pb ? AbeforeB : AafterB;
  27. }
  28.  
  29.  
  30. int main(void) {
  31. int i = 0, j = 0, count;
  32. char str[25][25], temp[25];
  33. while (1) {
  34. gets(str[i]);
  35. if (str[i][0] == '0') break;
  36. i++;
  37. }
  38. count = i;
  39. for (i = 0; i < count; i++) {
  40. for (j = i + 1; j < count; j++) {
  41. if (tailored_strcmp(str[i], str[j]) > 0) {
  42. strcpy(temp, str[i]);
  43. strcpy(str[i], str[j]);
  44. strcpy(str[j], temp);
  45. }
  46. }
  47. }
  48. for (i = 0; i < count; i++) {
  49. printf("[%s] ", str[i]);
  50. }
  51.  
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0s 4332KB
stdin
Ggxx
Ggdddddd
GGGGG
ggggg

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