DIY: Count a Word in the Comments

Solve the interview question "Count a Word in the Comments" in this lesson.

Problem statement

In this challenge, you are given two arguments: a list of strings containing lines of a C++ source code and a string containing a word. Your task is to identify which parts of the code are comments, and then count all occurrences of the input word present in the comments of the given source code.


The first input will be a list of strings containing the lines of source code. The second input will be a string representing the word that needs to be found in the code comments. The following is an example input:

["/* Example code for feature */", "int main() {", " /*", " This is a", " block comment in the code", " */", " int value = 10; // This is an inline comment", " int sum = value + /* this is also a block */ value;", " char code = 'c';", " return 0;", "}"]


In the example above, the list represents the following code:

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.