New update is available. Click here to update.

# Remove Comments

Contributed by
Amit Priyankar
Last Updated: 23 Feb, 2023
Medium
0/80
Avg time to solve 15 mins
Success Rate 85 %
Share
0 upvotes

## Problem Statement

#### Your task is to remove the comments from the given text and print the text in the same format.

##### Note:
``````The first comment is given precedence over the other i.e. if the string “//” occurs in a block comment, then it is ignored and/or if the string “/*” occurs in a line or a block comment, it is also ignored.

The starting and ending point of the block comment must be non-overlapping i.e. a string like “/*/” does not end the block comment, as the ending would be overlapping the beginning.

Every starting point of a block comment will have an ending point as well.

The (implicit) newline characters which may occur when a block comment spans multiple lines, must be deleted/removed.

The text doesn’t contain any single or double quotes characters.
``````
Detailed explanation ( Input/output format, Notes, Images )
##### Constraints:
``````1 <= N <= 100
0 <= Length of ith String <= 5 * 10^3

Where  ‘N’ represents the number of lines in the given text.

Time Limit: 1 sec
``````
##### Sample Input 1:
``````8
/*Sample
test case */
main() {
// Declare variables here.
int a = 2, b = 3, sum;
sum = a + b; // Find sum
Return;
}
``````
##### Sample Output 1:
``````main() {
int a = 2, b = 3, sum;
sum = a + b;
Return;
}
``````
##### Explanation 1:
``````Remove the line and block comments and print the resulting text. On removing comments, lines 1, 2 and 4 of the text becomes empty, so we do not output those lines.
``````
##### Sample Input 2:
``````2
Coding/* Block comment
Spanning multiple lines */ Ninjas
``````
##### Sample Output 2:
``````Coding Ninjas
``````
##### Explanation 2:
``````Remove the block comments and print the resulting text. On removing the block comment, the implicit newline character also gets removed.
``````
Auto
Console