Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <conio.h>
  3. #include <Windows.h>
  4. #include <malloc.h>
  5. int vvod(char* str)
  6. {
  7. int point = 0;
  8. int i = 0;
  9. fflush(stdin);
  10. while (1)
  11. {
  12.  
  13. str[i] = getchar();
  14.  
  15. if (str[i] == ' ')
  16. point = i;
  17. if (str[i] == '\n' && point != 0)
  18. {
  19. str[i] = '\0';
  20. break;
  21. }
  22. if (str[i] == '\n' && point == 0)
  23. {
  24. str[i] = str[i + 1];
  25. continue;
  26. }
  27. str = (char*)realloc(str, (i + 2) * sizeof(char));
  28. i++;
  29. }
  30. return i;
  31. }
  32. void exchange(char* str, int size)
  33. {
  34. char temp;
  35. int n_sl1, k_sl1, n_sl2, k_sl2, i = 0, j;
  36. while (str[i] == ' ') i++;
  37. n_sl1 = i;
  38. while (str[i] != ' ' && str[i] != '\0') i++;
  39. k_sl1 = i - 1;
  40. i = 0;
  41. while (str[size - i - 1] == ' ') i--;
  42. k_sl2 = size - i - 1 - 1;
  43. while (str[size - i - 1] != ' ') i--;
  44. n_sl2 = size - i - 1 - 1;
  45. if (k_sl1 - n_sl1 > k_sl2 - n_sl2) //первое слово длиннее второго
  46. {
  47. while (k_sl2 - n_sl2 >= 0)
  48. {
  49. temp = str[n_sl1];
  50. str[n_sl1] = str[n_sl2];
  51. str[n_sl2] = temp;
  52. n_sl1++;
  53. n_sl2++;
  54. }
  55. while (k_sl1 - n_sl1 >= 0)
  56. {
  57. j = k_sl1--;
  58. temp = str[j];
  59. while (j <= k_sl2) str[j++] = str[j + 1];
  60. str[k_sl2--] = temp;
  61. }
  62. }
  63. else //второе слово длиннее первого
  64. {
  65. while (k_sl1 - n_sl1 >= 0)
  66. {
  67. temp = str[n_sl1];
  68. str[n_sl1] = str[n_sl2];
  69. str[n_sl2] = temp;
  70. n_sl1++;
  71. n_sl2++;
  72. }
  73. while (k_sl2 - n_sl2 >= 0)
  74. {
  75. j = n_sl2++;
  76. temp = str[j];
  77. while (j >= n_sl1) str[j--] = str[j - 1];
  78. str[n_sl1++] = temp;
  79. }
  80. }
  81. }
  82. int main()
  83. {
  84. int i = 0, point=0, size;
  85. char* str = (char*)malloc(1 * sizeof(char));
  86. size=vvod(str);
  87. puts(str);
  88. exchange(str, size);
  89. puts(str);
  90. free(str);
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement