给定一个C / C++程序,从其中删除注释。
强烈建议最小化您的浏览器,然后自己尝试。
想法是维护两个标志变量,一个指示开始单行注释,另一个指示开始多行注释。设置标记后,我们将查找注释的结尾,并忽略开始和结尾之间的所有字符。
以下是上述想法的C++实现。
C
// C++ program to remove comments from a C/C++ program
#include
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
输出
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;
}
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。