fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int tailored_strcmp(const char *a, const char *b) {
  5. static char baseorder[] = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
  6. if (*a && *b) {
  7. //both strings have at least 1 character
  8. char *pa = strchr(baseorder, *a);
  9. if (!pa) return -1;
  10. char *pb = strchr(baseorder, *b);
  11. if (!pb) return 1;
  12. if (pa == pb) {
  13. //need to check second character of a and b
  14. char *ppa = strchr(baseorder, a[1]);
  15. if (!ppa) return -1;
  16. char *ppb = strchr(baseorder, b[1]);
  17. if (!ppb) return 1;
  18. if (ppa < ppb) return -1;
  19. if (ppa > ppb) return 1;
  20. return 0; // ignore anything beyond the 2nd character
  21. } else {
  22. if (pa < pb) return -1;
  23. return 1;
  24. }
  25. } else {
  26. //at least one string is the empty string
  27. if (*a == *b) return 0; // both empty strings
  28. if (*a) return -1; // b empty string, comes before a
  29. return 1; // a empty string
  30. }
  31. }
  32.  
  33.  
  34. int main(void) {
  35. int i = 0, j = 0, count;
  36. char str[25][25], temp[25];
  37. while (1) {
  38. gets(str[i]);
  39. if (str[i][0] == '0') break;
  40. i++;
  41. }
  42. count = i;
  43. for (i = 0; i < count; i++) {
  44. for (j = i + 1; j < count; j++) {
  45. if (tailored_strcmp(str[i], str[j]) > 0) {
  46. strcpy(temp, str[i]);
  47. strcpy(str[i], str[j]);
  48. strcpy(str[j], temp);
  49. }
  50. }
  51. }
  52. for (i = 0; i < count; i++) {
  53. printf("%s ", str[i]);
  54. }
  55.  
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0s 4284KB
stdin
alireza
Mohammad
Arash
anahita
sarah
Milad
john
Alireza
Maryam
ggg
gGg
Ggg
GGg
gGWWWWW
0
stdout
Alireza Arash alireza anahita GGg Ggg gGg gGWWWWW ggg john Maryam Milad Mohammad sarah