Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <string.h>
- bool is_delimiter(char t) {
- if (t == ' ' || t == ',' || t == ':' || t == '-' || t == ';' || t == '.') {
- return true;
- }
- return false;
- }
- int main() {
- char s[101];
- if (fgets(s, 101, stdin) == NULL) {
- perror("Couldn't read string");
- return 1;
- }
- char *words[100];
- int words_count = 0;
- int prev_pos = 0;
- if (is_delimiter(s[0])) {
- prev_pos = 1;
- }
- int pos = 1;
- while (s[pos] != '\0' && s[pos] != '\n') {
- if (is_delimiter(s[pos])) {
- if (!is_delimiter(s[pos - 1])) {
- words[words_count] = calloc(pos - prev_pos + 1, sizeof(char));
- memcpy(words[words_count], &s[prev_pos], pos - prev_pos);
- // printf("|%s|\n", words[words_count]);
- ++words_count;
- }
- prev_pos = pos + 1;
- }
- ++pos;
- }
- if (!is_delimiter(s[pos - 1])) {
- words[words_count] = calloc(pos - prev_pos + 1, sizeof(char));
- memcpy(words[words_count], &s[prev_pos], pos - prev_pos);
- ++words_count;
- }
- for (int i = 0; i < words_count; ++i) {
- int count = 0;
- for (int j = 0; j < words_count; ++j) {
- if (i != j && strcmp(words[i], words[j]) == 0) {
- ++count;
- words[j][0] = '\0';
- }
- }
- if (count && words[i][0] != '\0') {
- printf("%s\n", words[i]);
- }
- }
- for (int j = 0; j < words_count; ++j) {
- free(words[j]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement