Remove comments from a given C/C++ program - GeeksQuiz
Given a C/C++ program, remove comments from it.
this program is not taking care that if // or /* present inside a defined string, as string s="ghchgc//nbnbnbb"; then it will remove string after //
Finding Comments in Source Code Using Regular Expressions
http://blog.ostermiller.org/find-comment
http://rosettacode.org/wiki/Strip_block_comments#Java
Read full article from Remove comments from a given C/C++ program - GeeksQuiz
Given a C/C++ program, remove comments from it.
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.
string removeComments(string prgm){ int n = prgm.length(); string res; // Flags to indicate that single line and multpile 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 approproate 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;}this program is not taking care that if // or /* present inside a defined string, as string s="ghchgc//nbnbnbb"; then it will remove string after //
Finding Comments in Source Code Using Regular Expressions
http://blog.ostermiller.org/find-comment
http://rosettacode.org/wiki/Strip_block_comments#Java
Read full article from Remove comments from a given C/C++ program - GeeksQuiz