VOOZH about

URL: https://www.geeksforgeeks.org/cpp/remove-comments-given-cc-program/

⇱ Remove comments from a given C/C++ program - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Remove comments from a given C/C++ program

Last Updated : 31 Jan, 2023

Given a C/C++ program, remove comments from it. 

We strongly recommend to minimize your browser and try this yourself first.

The idea is to maintain two flag variables, one to indicate that a single line comment is started, another to indicate that a multiline comment is started. When a flag is set, we look for the end of comment and ignore all characters between start and end.

Following is C++ implementation of above idea.


Output
Given Program 
 /* Test program */ 
 int main() 
 { 
 // variable declaration 
 int a, b, c; 
 /* This is a test 
 multiline 
 comment for 
 testing */ 
 a = b + c; 
 } 

 Modified Program 
 int main() 
 { 
 int a, b, c; 
 
 a = b + c; 
 } 

Time  Complexity :O(N), where N is length of string.

Space Complexity: O(1), since no extra space used.

Comment
Article Tags:
Article Tags: