Remove comments


From a C++ program remove all the comments. The program source is an array where source[i] is the i-th line of source code. The array represents the result obtained by splitting the original code by newline character \n.


There are 2 types of comments in C++, line comments, and block comments.
 This '//' denotes a line comment, which represents that it and the rest of the characters to the right of it in the same line should be ignored.


The string '/*' denotes a block comment, which represents that all characters until the next (non-overlapping) occurrence of */ should be ignored. (The string /*/ does not yet end the block comment, as the ending would be overlapping the beginning).
The first effective comment takes precedence over others: if the string // occurs in a block comment, it is ignored. similarly, if string /* occur in a line or block comment, it is also ignored.


If a certain line of code is empty after removing comments, you must not output that line, i.e each string in the result will be a non-empty string.


There will be no control characters, single quote or double quote characters also defines or macros will not interfere with comments.


It is guaranteed that every open block will be closed.
Newline characters can be deleted by block comments.
After removing the comments return the source code in the same format.


Input:
source = ["/*Hello world */", "int main()", "{" ,"// print<< ", "Hello world;"]
output = ["int main", "{", "Hello world";]


Solution:

We maintain a variable startBlock which marks the start of a block comment and everything after that is ignored until the block comment is closed.

If we get a line comment we ignore the rest of the string.

In the end, we check if the string is empty before putting in the result.

Code:

vector<string> removeComments(vector<string>& source) {

        vector<string> output;

        string s="";

        int startBlock=0;

        for(int i=0;i<source.size();i++)

        {

            if(startBlock==0)

            s="";

            for(int j=0;j<source[i].length();j++)

            {

                if(startBlock==1)

                {

                    while(j<source[i].length())

                   {

                       if(source[i][j]=='*'&&(j+1<=source[i].length())&&(source[i][j+1]=='/'))                        {

                           startBlock=0;

                           j++;

                          break;

                       }

                           j++;

                          

                    }

                    continue;

                }

               if(source[i][j]=='/'&&(j+1<=source[i].length())&&(source[i][j+1]=='/'||source[i][j+1]=='*'))

                {

                   if(source[i][j+1]=='/')

                       break;

                   startBlock=1;

                   j+=2;

                   while(j<source[i].length())

                   {

                       if(source[i][j]=='*'&&(j+1<=source[i].length())&&(source[i][j+1]=='/'))                        {

                           startBlock=0;

                           j++;

                          break;

                       }

                           j++;

                          

                    }

                }

                else

                     s+=source[i][j];

            }

            if(!s.empty()&&startBlock==0)

                output.push_back(s);

        }

        return output;

    }

Do share the post if you find it helpful and comment if you have some new insights about the problem.
Happy Programming

Previous
Next Post »

If you have any doubts or any new insights, please share ConversionConversion EmoticonEmoticon

Sum of Subarray Minimums

Given an array A of integers, find the sum of min(B), where B ranges over every (contiguous) subarray of A. Since the answer may be large, r...