I'm given a sequence containing from 3 to 30 words. Printing all words that occur more than once in the sequence.

I'm given a sequence containing from 3 to 30 words, each of which contains from 1 to 5 capital Latin letters; between adjacent words - a comma, after the last word - a period. Type: all words that occur more than once in the sequence.

There is a ready-made code, but help is needed, I do not quite understand it, I would be very grateful if you could help me understand, suggested some points, what is responsible for and how it works.

sorry for C, not C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <locale.h>
#include <string.h>
 
#define S 256
#define W 30
#define Z ",."
 
int main (void){
    int i, j, c = 0;
    char str[S], *ptr = 0, *word[W] = {0};
    do{
        printf ("You are given a sequence containing from 3 to 30 words, each of which contains from 1 to 5 capital Latin letters; between adjacent words - a comma, after the last word - a period. Print: all words that occur more than once in the sequence.\n\n");
        
        printf ("Enter the string: \n");
        if (scanf("%255[^\n]", str) == 1 && fgetc(stdin) == '\n'){
            
            for (ptr = strtok(str,Z); ptr != 0 && c < W; c++, ptr = strtok(0,Z))
            word[c] = ptr;
            
            printf ("Words:\n");
            for (i = 0; i < c; i++)
            printf ("%s\n", word[i]);
            printf ("\n");
            printf ("Duplicate words:\n");
            
            for (i = 0; i < c; i++){
                for (j = 0; j < c; j++){
                    if (i != j && strcmp(word[i], word[j]) == 0)
                        printf("%s\n", word[i]);
                }
            }
        }
    }
    while (getchar() != 27);
}
Last edited on
I do not quite understand it


Then trace through it using the debugger to see how it works.
how can i do this in Xcode ?
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
if Xcode lacks a friendly debugger, then just insert some print statements into the code to see what it is doing at each step to see what it is doing (short term solution) while you research either how to better use Xcode (if it has what you need) or what else you could use instead (if it does not).
Topic archived. No new replies allowed.