Advertisement
gg-master

Untitled

Apr 27th, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. int main() {
  6. const short int length = 80;
  7. char str[81] = "";
  8. char words[40][21] = {};
  9. int words_length[length] = {};
  10.  
  11. gets_s(str, length);
  12.  
  13. // Получаем массив с выделенными словами и введенного предложения
  14. char seps[] = "!'\";:?-., "; // строка разделителей
  15.  
  16. // Копирование исходной строки, чтобы избежать изменения исходной строки
  17. char copy_str[81] = "";
  18. strcpy(copy_str, str);
  19.  
  20. char* token; // ссылка на начало подстроки
  21.  
  22. token = strtok(copy_str, seps); // выделение первой подстроки
  23. int count_words = 0;
  24. while(token != NULL)
  25. {
  26. char word[21] = "";
  27. strcpy(word, token); // Выделяем слово
  28.  
  29. // Проверяем, содержит ли слово недопустимый символ
  30. int is_ok = 1;
  31. char *symbol = word;
  32. while (*symbol != '\0' && is_ok)
  33. {
  34. is_ok = isalnum(symbol[0]);
  35. symbol++;
  36. }
  37.  
  38. if (is_ok)
  39. // Если слово не содержит недопустимый символ, то добавляем его в массив слов
  40. strcpy(words[count_words++], word);
  41. token = strtok(NULL, seps); // выделение следующей подстроки
  42. }
  43. // Сортируем массив со словами в порядке убывания их длин
  44. for (int i = 0; i < count_words - 1; i++)
  45. {
  46. for (int j = 0; j < count_words - 1 - i; j++)
  47. {
  48. if (strlen(words[j]) < strlen(words[j + 1]))
  49. {
  50. char tmp[21] = ""; // буфер для обмена словами
  51. strcpy(tmp, words[j]);
  52. strcpy(words[j], words[j + 1]);
  53. strcpy(words[j + 1], tmp);
  54. }
  55. }
  56. }
  57.  
  58. // Печатаем результат в консоль
  59. for (int i = 0; i < count_words; i++)
  60. {
  61. printf("%s", words[i]);
  62. printf("\n");
  63. }
  64. if (!count_words)
  65. printf("no solution");
  66. return 0;
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement