DIY: Count a Word in the Comments

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

We'll cover the following

Problem statement

In this challenge, you are given two arguments: an array 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.

Input

The first input will be an array 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;", "}"]

"code"

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

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