← 返回 microsoft 的题目列表Remove All Comments from Source String (C++)
类型:online_judge
Problem: Remove All Comments from Source (String In / String Out)
Given a piece of source code as input text (a string that may contain newlines), remove all comments and output the resulting source.
Comment rules:
Line comments start with // and go until the end of the line (excluding the newline).
Block comments start with /* and end at the nearest */, and may span multiple lines.
After removing comments, preserve the order of non-comment characters and keep the newline structure (no need to trim extra spaces).
Input
Multiple lines from stdin representing the source text.
Output
The source text with all comments removed.
Constraints
Total length n <= 2 * 10^5
Block comments are guaranteed to be closed.
Example
Input:
int main() {
// initialize
int a = 1; /* set a */
return a;
}
Output:
int main() {
int a = 1;
return a;
}
Example
Input
int main() {
// initialize
int a = 1; /* set a */
return a;
}
Output
int main() {
int a = 1;
return a;
}