https://github.com/xieqilu/Qilu-leetcode/blob/master/B200.IdentifyComments.cs
Jack wants to build an IDE on his own. Help him build a feature which identifies the comments, in the source code of computer programs. Assume, that the programs are written either in C, C++ or Java. The commenting conventions are displayed here, for your convenience. At this point of time you only need to handle simple and common kinds of comments. You don't need to handle nested comments, or multi-line comments inside single comments or single-comments inside multi-line comments.
Your task is to write a program, which accepts as input, a C or C++ or Java program and outputs only the comments from those programs. Your program will be tested on source codes of not more than 200 lines.
Comments in C, C++ and Java programs
Single Line Comments:
// this is a single line comment
x = 1; // a single line comment after code
Please note that in the real world, some C compilers do not necessarily support the above kind of comment(s) but for the purpose of this problem let's just assume that the compiler which will be used here will accept these kind of comments.
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
string s = "";
bool isComment = false;
while((s=Console.ReadLine())!=null){
s=s.Trim(); //remove leading and trailing spaces
if(s.Contains("//")){
Console.WriteLine(s.Substring(s.IndexOf("//")));
}
else if(s.Contains("*/")){
Console.WriteLine(s);
isComment=false;
}
else if(isComment)
Console.WriteLine(s);
else if(s.Contains("/*")){
Console.WriteLine(s);
isComment=true;
}
}
Jack wants to build an IDE on his own. Help him build a feature which identifies the comments, in the source code of computer programs. Assume, that the programs are written either in C, C++ or Java. The commenting conventions are displayed here, for your convenience. At this point of time you only need to handle simple and common kinds of comments. You don't need to handle nested comments, or multi-line comments inside single comments or single-comments inside multi-line comments.
Your task is to write a program, which accepts as input, a C or C++ or Java program and outputs only the comments from those programs. Your program will be tested on source codes of not more than 200 lines.
Comments in C, C++ and Java programs
Single Line Comments:
// this is a single line comment
x = 1; // a single line comment after code
Please note that in the real world, some C compilers do not necessarily support the above kind of comment(s) but for the purpose of this problem let's just assume that the compiler which will be used here will accept these kind of comments.
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
string s = "";
bool isComment = false;
while((s=Console.ReadLine())!=null){
s=s.Trim(); //remove leading and trailing spaces
if(s.Contains("//")){
Console.WriteLine(s.Substring(s.IndexOf("//")));
}
else if(s.Contains("*/")){
Console.WriteLine(s);
isComment=false;
}
else if(isComment)
Console.WriteLine(s);
else if(s.Contains("/*")){
Console.WriteLine(s);
isComment=true;
}
}