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++
#include <iostream>
using namespace std;
string removeComments(string prgm)
{
int n = prgm.length();
string res;
bool s_cmt = false ;
bool m_cmt = false ;
for ( int i=0; i<n; i++)
{
if (s_cmt == true && prgm[i] == '\n' )
s_cmt = false ;
else if (m_cmt == true && prgm[i] == '*' && prgm[i+1] == '/' )
m_cmt = false , i++;
else if (s_cmt || m_cmt)
continue ;
else if (prgm[i] == '/' && prgm[i+1] == '/' )
s_cmt = true , i++;
else if (prgm[i] == '/' && prgm[i+1] == '*' )
m_cmt = true , i++;
else res += prgm[i];
}
return res;
}
int 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;
}
|
OutputGiven 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.