fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_LEN 256
  5. #define MAX_WORDS 50
  6.  
  7. int main() {
  8. char str[MAX_LEN];
  9. char words[MAX_WORDS][MAX_LEN];
  10. int wordCount = 0;
  11.  
  12. printf("Введіть речення: ");
  13. fgets(str, MAX_LEN, stdin);
  14.  
  15. str[strcspn(str, "\n")] = 0;
  16.  
  17. char *token = strtok(str, " ,.!?;:");
  18. while (token != NULL && wordCount < MAX_WORDS) {
  19. strcpy(words[wordCount], token);
  20. wordCount++;
  21. token = strtok(NULL, " ,.!?;:");
  22. }
  23.  
  24. for (int i = 0; i < wordCount - 1; i++) {
  25. for (int j = i + 1; j < wordCount; j++) {
  26. if (strcmp(words[i], words[j]) > 0) {
  27. char temp[MAX_LEN];
  28. strcpy(temp, words[i]);
  29. strcpy(words[i], words[j]);
  30. strcpy(words[j], temp);
  31. }
  32. }
  33. }
  34.  
  35. printf("Слова у відсортованому порядку:\n");
  36. for (int i = 0; i < wordCount; i++) {
  37. printf("%s\n", words[i]);
  38. }
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5320KB
stdin
банан яблуко апельсин вишня
stdout
Введіть речення: Слова у відсортованому порядку:
апельсин
банан
вишня
яблуко