fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. char text[200] = "Сегодня ясно. Будет солнце! Завтра холодно?";
  5. char words[20][30]; // массив слов
  6. int i = 0, j = 0, k = 0, word_count = 0;
  7. char c;
  8.  
  9. printf("Исходный текст: %s\n", text);
  10. printf("\nПосле сортировки слов:\n");
  11.  
  12. while (text[i] != '\0') {
  13. word_count = 0;
  14. k = 0;
  15.  
  16. // Собираем слова до конца предложения
  17. while (text[i] != '\0' && text[i] != '.' && text[i] != '!' && text[i] != '?') {
  18. if (text[i] == ' ') {
  19. if (k > 0) {
  20. words[word_count][k] = '\0';
  21. word_count++;
  22. k = 0;
  23. }
  24. } else {
  25. words[word_count][k] = text[i];
  26. k++;
  27. }
  28. i++;
  29. }
  30. // Последнее слово
  31. if (k > 0) {
  32. words[word_count][k] = '\0';
  33. word_count++;
  34. }
  35.  
  36. // Запоминаем конец предложения
  37. c = text[i];
  38. if (text[i] != '\0') i++;
  39.  
  40. // Сортируем слова (пузырьком)
  41. for (int m = 0; m < word_count - 1; m++) {
  42. for (int n = 0; n < word_count - 1 - m; n++) {
  43. int p = 0;
  44. while (words[n][p] != '\0' && words[n+1][p] != '\0' && words[n][p] == words[n+1][p]) {
  45. p++;
  46. }
  47. if (words[n][p] > words[n+1][p]) {
  48. // Меняем слова местами
  49. char temp[30];
  50. int t = 0;
  51. while (words[n][t] != '\0') {
  52. temp[t] = words[n][t];
  53. t++;
  54. }
  55. temp[t] = '\0';
  56.  
  57. t = 0;
  58. while (words[n+1][t] != '\0') {
  59. words[n][t] = words[n+1][t];
  60. t++;
  61. }
  62. words[n][t] = '\0';
  63.  
  64. t = 0;
  65. while (temp[t] != '\0') {
  66. words[n+1][t] = temp[t];
  67. t++;
  68. }
  69. words[n+1][t] = '\0';
  70. }
  71. }
  72. }
  73.  
  74. // Выводим отсортированное предложение
  75. for (int m = 0; m < word_count; m++) {
  76. printf("%s", words[m]);
  77. if (m < word_count - 1) printf(" ");
  78. }
  79. if (c != '\0') printf("%c ", c);
  80. }
  81.  
  82. printf("\n");
  83. return 0;
  84. }
  85.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Исходный текст: Сегодня ясно. Будет солнце! Завтра холодно?

После сортировки слов:
Сегодня ясно. Будет солнце! Завтра холодно?