Advertisement
leviathan0117

Untitled

Jun 25th, 2022 (edited)
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. bool is_delimiter(char t) {
  7.     if (t == ' ' || t == ',' || t == ':' || t == '-' || t == ';' || t == '.') {
  8.         return true;
  9.     }
  10.     return false;
  11. }
  12.  
  13. int main() {
  14.     char s[101];
  15.     if (fgets(s, 101, stdin) == NULL) {
  16.         perror("Couldn't read string");
  17.         return 1;
  18.     }
  19.     char *words[100];
  20.     int words_count = 0;
  21.     int prev_pos = 0;
  22.     if (is_delimiter(s[0])) {
  23.         prev_pos = 1;
  24.     }
  25.  
  26.     int pos = 1;
  27.     while (s[pos] != '\0' && s[pos] != '\n') {
  28.         if (is_delimiter(s[pos])) {
  29.             if (!is_delimiter(s[pos - 1])) {
  30.                 words[words_count] = calloc(pos - prev_pos + 1, sizeof(char));
  31.                 memcpy(words[words_count], &s[prev_pos], pos - prev_pos);
  32.                 // printf("|%s|\n", words[words_count]);
  33.                 ++words_count;
  34.             }
  35.             prev_pos = pos + 1;
  36.         }
  37.         ++pos;
  38.     }
  39.     if (!is_delimiter(s[pos - 1])) {
  40.         words[words_count] = calloc(pos - prev_pos + 1, sizeof(char));
  41.         memcpy(words[words_count], &s[prev_pos], pos - prev_pos);
  42.         ++words_count;
  43.     }
  44.  
  45.     for (int i = 0; i < words_count; ++i) {
  46.         int count = 0;
  47.         for (int j = 0; j < words_count; ++j) {
  48.             if (i != j && strcmp(words[i], words[j]) == 0) {
  49.                 ++count;
  50.                 words[j][0] = '\0';
  51.             }
  52.         }
  53.         if (count && words[i][0] != '\0') {
  54.             printf("%s\n", words[i]);
  55.         }
  56.     }
  57.    
  58.     for (int j = 0; j < words_count; ++j) {
  59.         free(words[j]);
  60.     }
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement