Need help commenting out the code

Hello, so i have a task:
I have a sequence which contains from 3 to 30 words, in each of which from 1 to 5 spelled Latin letters; between adjacent words - a comma, after the last word - a dot. I need to type all words that occur in sequence more than once.

I have a ready code (i made it on C, there's no strong differences from C++):

#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);
}


I need your help by commenting the code, would be very grateful for that.
you can comment out code 2 ways.
1) if the segment of code does not contain a /* */ set, you can put that around it and it will be commented out.
2) if it does, you can use #defines.
#ifdef thisdoesnotexist
code
#endif
to get the code back you can define the variable in the project or remove the define block (both start and end have to go).
In fact, the problem is a little different, I can comment, but here I need your help, so that you can tell me what the line is for and what is its meaning.
If you wrote the code, wouldn't you know what the code does?
What parts of the code don't you understand?
Last edited on
27 is the 'escape' character.
other than that, it reads in, splits up, and then brute force checks for repeats by comparing everything against everything.
Do you know C, or did you have a specific question? How did you get this homework looking code that you do not understand?
The thing is that I am a student, and we were given this code and we need to figure it out, so I ask your help to comment on the code so that I can better understand how this program works.
I am not going to line by line it, but
printf is just printing to the screen, text output for the user.
scanf is the reverse, it reads from the user/keyboard.
strtok breaks a string into tokens, or in this case, 'words'.
strcmp compares two strings, the result is 0 when they are equal. c is case aware, so "hello" does not equal "Hello".

so in a nutshell:
print some stuff,
read from the user a line of text
split it into 'words'

and then do a pair of for loops that
compare the first word against every other word except itself, the the second word, ... for all the words.

it seems wrong, actually, it seems like it should have been for (j = i+1 .... etc) to avoid duplicate output. And its unclear why he coded it to use brute force, its a poor way to solve the problem.

the entire works is wrapped up in a do while to repeat entering more sentences and checking them until you press escape to stop. this is the do-while loop around everything.
Last edited on
It helps if you format the code so that it's readable and use code tags:

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
37
38
#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);
}


str is the input line
word is an array of pointers into str
The first for loop uses strtok to produce the word array.
The pair of for loops check for duplicated words
Topic archived. No new replies allowed.