Remove comments from a given C/C++ program

23 Jun, 2022
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.
C++
// C++ program to remove comments from a C/C++ program#include <iostream>using namespace std; string removeComments(string prgm){    int n = prgm.length();    string res;     // Flags to indicate that single line and multiple line comments    // have started or not.    bool s_cmt = false;    bool m_cmt = false;      // Traverse the given program    for (int i=0; i<n; i++)    {        // If single line comment flag is on, then check for end of it        if (s_cmt == true && prgm[i] == '\n')            s_cmt = false;         // If multiple line comment is on, then check for end of it        else if  (m_cmt == true && prgm[i] == '*' && prgm[i+1] == '/')            m_cmt = false,  i++;         // If this character is in a comment, ignore it        else if (s_cmt || m_cmt)            continue;         // Check for beginning of comments and set the appropriate flags        else if (prgm[i] == '/' && prgm[i+1] == '/')            s_cmt = true, i++;        else if (prgm[i] == '/' && prgm[i+1] == '*')            m_cmt = true,  i++;         // If current character is a non-comment character, append it to res        else  res += prgm[i];    }    return res;} // Driver program to test above functionsint main(){    string prgm = "   /* Test program */ \n"                  "   int main()  \n"                  "   {           \n"                  "      // variable declaration \n"                  "      int a, b, c;    \n"                  "      /* This is a test  \n"                  "          multiline     \n"                  "          comment for   \n"                  "          testing */      \n"                  "      a = b + c;       \n"                  "   }           \n";    cout << "Given Program \n";    cout << prgm << endl;    cout << " Modified Program ";    cout << removeComments(prgm);    return 0;}
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;       
   }           

simranarora5sos
hardikkoriintern
BrowserStack
C++
Strings
BrowserStack
Strings
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Priority Queue in C++ Standard Template Library (STL)
Set in C++ Standard Template Library (STL)
vector erase() and clear() in C++
Substring in C++
unordered_map in C++ STL
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Check for Balanced Brackets in an expression (well-formedness) using Stack