[Solved] How does this code counting the number of characters, lines, and words work? [closed]


strlen( line )

Give the number of characters in line. (Check man strlen.)

num_chars += strlen( line )

Add that number to num_chars.

strncmp( line, "", MAX_LINE_LEN )

Compare the contents of line with the empty string (but just to be on the safe side, do not read more than MAX_LINE_LEN characters of line, in case it is not null-terminated). Return 0 if equal. (For further details, refer to man strncmp.)

if (strncmp(line, "", MAX_LINE_LEN) != 0) {
    num_words++;
}

Add 1 to num_words if line does not equal the empty string.

Note that num_words is a misnomer, as there is no word counting going on here, just a counting of non-empty lines.

8

solved How does this code counting the number of characters, lines, and words work? [closed]