Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. void csort(char *src, char *dest)
  5. {
  6. int dlin = strlen(src),i = 0,j = 1,t = 0;
  7. while(i < dlin) {
  8. if (src[i] == 32) {
  9. if (t == 0) {
  10. j = j +1;
  11. }
  12. ++t;
  13. ++i;
  14. }
  15. else {
  16. ++i;
  17. t = 0;
  18. }
  19. }
  20. int *a = (int*)malloc((j+2) * sizeof(int));
  21. int *counts = (int*)malloc((j+2) * sizeof(int));
  22. for(i = 0;i<j;i++) {
  23. a[i] = 0;
  24. counts[i] = 0;
  25. }
  26. int *b = (int*)malloc((j+2) * sizeof(int));
  27. i = 0;
  28. j = 0;
  29. t = 0;
  30. int m = 0;
  31. while(i < dlin) {
  32. if (src[i] == 32) {
  33. if (t == 0) {
  34. j = j +1;
  35. }
  36. m = 0;
  37. ++t;
  38. ++i;
  39. }
  40. else {
  41. a[j] = a[j] + 1;
  42. if (m == 0) {
  43. b[j] = i;
  44. }
  45. ++i;
  46. ++m;
  47. t = 0;
  48. }
  49. }
  50. int n = j+1;
  51. j = 0;
  52. while (j < n-1) {
  53. i = j+1;
  54. while (i<n) {
  55. if (a[i] < a[j]) {
  56. counts[j] += 1;
  57. }
  58. else {
  59. counts[i] += 1;
  60. }
  61. ++i;
  62. }
  63. ++j;
  64. }
  65. t = 0;
  66. int flag = 0;
  67. for(i = 0;i < n;i++) {
  68. j = 0;
  69. while(flag == 0) {
  70. if (counts[j] == i) {
  71. for(m = 0;m<a[j];m++) {
  72. dest[t+m] = src[b[j]+m];
  73. }
  74. t = t + m + 1;
  75. flag = 1;
  76. }
  77. ++j;
  78. }
  79. flag = 0;
  80. if (i < n - 1) {
  81. dest[t-1] = 32;
  82. }
  83. }
  84. free(a);
  85. free(counts);
  86. free(b);
  87. }
  88.  
  89. int main()
  90. {
  91. char str[10000];
  92. char bufer[100];
  93. gets(str);
  94. csort(str,bufer);
  95. int b = 0;
  96. int g = strlen(bufer);
  97. for(int i = 0;i<g;i++) {
  98. printf("%c" , bufer[i]);
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement