Goal
Replace all comments in C, C++, or Java Code with the comment // Do the needful
.
Specifics
This coding challenge is a simple one. Take as input the name of a file. The input file shall contain C, C++, or Java code. The code shall contain one or more comments. The comments can be single line /* */
or //
delimited, as well as multi-line /* */
delimited. The output of the program should be identical to the input except all of the comments should be converted to // Do the needful
.
For example if the input file is:
#include <iostream.h>
int result; // the result of the calculations
char oper_char; // the user-specified operator
int value; // value specified after the operator
/* standard main function */
int main()
{
result = 0; // initialize the result
// Loop forever (or till we hit the break statement)
while (1) {
cout << "Result: " << result << '\n';
/* This code outputs display and requests
input from the user */
cout << "Enter operator and number: ";
cin >> oper_char;
cin >> value;
if (oper_char = '+') {
result += value;
} else {
cout << "Unknown operator " << oper_char << '\n';
}
}
return (0);
}
The output of the program should read
#include <iostream.h>
int result; // Do the needful
char oper_char; // Do the needful
int value; // Do the needful
// Do the needful
int main()
{
result = 0; // Do the needful
// Do the needful
while (1) {
cout << "Result: " << result << '\n';
// Do the needful
cout << "Enter operator and number: ";
cin >> oper_char;
cin >> value;
if (oper_char = '+') {
result += value;
} else {
cout << "Unknown operator " << oper_char << '\n';
}
}
return (0);
}
Scoring
This is a popularity contest. You get two extra votes added to your score if none of the following words appears in your program in any case variation: {"do", "the", "needful"}
. Score is number of votes plus bonus if applicable.
Allowances
If comments appear in string literals it is acceptable to convert them to // Do the needful
as well. After all... you can never have enough needful.
"d" + "o"
,"t" + "he"
and"need" + "ful"
? Also, does the submission have to be able to deal with any valid C, C++ or Java code? That's pretty harsh and amounts to writing a lexer for all three languages (I'm thinking about comment literals in strings and vice-versa.) If so, what about 3rd-party lexer libraries? \$\endgroup\$/* ... */
precedes a non-comment on a line? \$\endgroup\$char str[]="/**///";
or a comment-start sequence/*
appears in a/*
comment, or a backslash-newline appears in a//
comment, or a//
comment within a/**/
comment? \$\endgroup\$