Created
May 9, 2022 01:57
-
-
Save hakxcore/cd2bf754158e97e7dc60803c72183e78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Input file (input.cpp) | |
/* Program to add two numbers using cpp | |
Take three variables a, b, c | |
Input a, b from user | |
add a and b to c | |
print c | |
*/ | |
#include <bits/stdc++.h> | |
using namespace std; | |
int main() | |
{ | |
// Three variables a b c | |
int a, b, c; | |
cin >> a >> b; // Input a b from user | |
c = a + b; // add a b to c | |
cout << c; // Print output | |
return 0; | |
} | |
## CommentRemover code | |
#include <bits/stdc++.h> | |
using namespace std; | |
string removeComments(string prgm) | |
{ | |
int n = prgm.length(); | |
string res; | |
bool s_cmt = false; | |
bool m_cmt = false; | |
for (int i = 0; i < n; i++) | |
{ | |
if (s_cmt == true && prgm[i] == '\n') | |
{ | |
s_cmt = false; | |
} | |
else if (m_cmt == true && prgm[i] == '*' && prgm[i + 1] == '/') | |
{ | |
m_cmt = false, i++; | |
} | |
else if (s_cmt || m_cmt) | |
{ | |
continue; | |
} | |
else if (prgm[i] == '/' && prgm[i + 1] == '/') | |
{ | |
s_cmt = true, i++; | |
} | |
else if (prgm[i] == '/' && prgm[i + 1] == '*') | |
{ | |
m_cmt = true, i++; | |
} | |
else | |
{ | |
res += prgm[i]; | |
} | |
} | |
return res; | |
} | |
int main() | |
{ | |
ifstream InputFile("input.cpp"); | |
ofstream OutputFile("output_CmntsRemoved.cpp"); | |
string prgm; | |
string line; | |
while (getline(InputFile, line)) | |
{ | |
prgm += line; | |
prgm += "\n"; | |
} | |
cout << "Given Program \n"; | |
cout << prgm << endl; | |
cout << " Modified Program "; | |
string res = removeComments(prgm); | |
cout << res; | |
OutputFile << res; | |
return 0; | |
} | |
## Output | |
#include <bits/stdc++.h> | |
using namespace std; | |
int main() | |
{ | |
int a, b, c; | |
cin >> a >> b; c = a + b; cout << c; return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment