fork download
  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6.  
  7. #define MAX_WORDS 1000
  8. #define MAX_WORD_LEN 100
  9.  
  10. typedef struct {
  11. char word[MAX_WORD_LEN];
  12. int count;
  13. } WordCount;
  14.  
  15. int find_word(WordCount words[], int word_count, const char *word) {
  16. for (int i = 0; i < word_count; i++) {
  17. if (strcmp(words[i].word, word) == 0) {
  18. return i;
  19. }
  20. }
  21. return -1;
  22. }
  23.  
  24. int main() {
  25. char text[10000];
  26. WordCount words[MAX_WORDS];
  27. int word_count = 0;
  28.  
  29. printf("Введіть текст:\n");
  30. fgets(text, sizeof(text), stdin);
  31.  
  32. char *token = strtok(text, " \t\n\r");
  33. while (token != NULL) {
  34. int index = find_word(words, word_count, token);
  35. if (index >= 0) {
  36. words[index].count++;
  37. } else {
  38. strcpy(words[word_count].word, token);
  39. words[word_count].count = 1;
  40. word_count++;
  41. }
  42. token = strtok(NULL, " \t\n\r");
  43. }
  44.  
  45. printf("\nЧастота слів:\n");
  46. for (int i = 0; i < word_count; i++) {
  47. printf("%s: %d\n", words[i].word, words[i].count);
  48. }
  49.  
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
Введіть текст:

Частота слів: