0% found this document useful (0 votes)
17 views4 pages

MOHDCD

This document describes a student's program to detect and remove comments from code. It presents the theory behind identifying one-line and multi-line comments, provides sample code to determine the type of comment in a given string, and code to remove comments by ignoring text between delimiters like // and /* */. The output shows the comment-filtered code.

Uploaded by

Lovish Sheikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

MOHDCD

This document describes a student's program to detect and remove comments from code. It presents the theory behind identifying one-line and multi-line comments, provides sample code to determine the type of comment in a given string, and code to remove comments by ignoring text between delimiters like // and /* */. The output shows the comment-filtered code.

Uploaded by

Lovish Sheikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: MOHD SHAHJAHAN CSE-B Roll No: 2100320100104

EXPERIMENT-3

Program: Write a program to detect the comments.

Theory: The aim is to determine whether or not a given string, S, which represents a line in a program, is a
comment.

types of program comments:

Comments on one line must be followed by a double slash ('//').

Comments that span multiple lines are those that begin with ('/*') and end with ('*/').

Method: The goal is to determine whether or not the input string is a comment. Here are the actions:

If the value at the first index, which is index 0, is '/,' then follow the instructions below; otherwise, print "It is
not a comment."

if line[0] equals '/'

Print "It is a single line comment" if line[1] == '/'.

If line[1] == '*', then look through the string and display "It is a multi-line comment" if any adjacent pair of '*'
& '/' is found.
If not, type "It is not a comment" in print.

Code:
#include <bits/stdc++.h>
using namespace std;

void comment() // identify the comment


{
int n;
cin >> n;
vector<string> st;
for (int j = 0; j < n; j++)
{
string s;
cin >> s;
st.push_back(s);
// cout << st[j];
}
int f = 0;
string ans = "";
Name: MOHD SHAHJAHAN CSE-B Roll No: 2100320100104

EXPERIMENT-3

for (int i = 0; i < n; i++)


{
ans += st[i];
}

if (ans[0] == '/' && ans[1] == '/')


{
cout << "singal line comment" << endl;
return;
}
else if ((ans[0] == '/' && ans[1] == '*') && (ans[ans.length() - 2] == '*' &&
ans[ans.length() - 1] == '/'))
{
cout << "multiline comment" << endl;
return;
}
cout << "No comment" << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
comment();
}

Output:
Name: MOHD SHAHJAHAN CSE-B Roll No: 2100320100104

EXPERIMENT-3

Program: Write a program to remove the comments.

Theory: The plan is to keep two flag variables, one to signal the beginning of a single line comment and
another to signal the beginning of a multiline comment. When a flag is set, we scan the comment for its end
and ignore all characters in between

Code:

#include <bits/stdc++.h> //remmove the comment


using namespace std;

string remove(string s, int i)


{
string ans = "";
int n = s.length();
if (i == 0)
{
ans = s.substr(2, n);
}
else
{
ans = s.substr(0, n - 2);
}
return ans;
}

void comment()
{
string ans = "";
int n;
cin >> n;
vector<string> st, st2;
for (int j = 0; j < n; j++)
{
string s;
cin >> s;
st.push_back(s);
}
for (int i = 0; i < n; i++)
{
if ((i == 0) && ((st[i][0] == '/' && st[i][1] == '/') || (st[i][0] == '/'
&& st[i][1] == '*')))
{
ans = remove(st[i], i);
Name: MOHD SHAHJAHAN CSE-B Roll No: 2100320100104

EXPERIMENT-3

st2.push_back(ans);
}
else if ((i == n - 1) && (st[i][st[i].length() - 2] == '*' &&
st[i][st[i].length() - 1] == '/'))
{

ans = remove(st[i], i);


st2.push_back(ans);
//}
}
else
st2.push_back(st[i]);
}
for (int j = 0; j < n; j++)
{
cout << st2[j] << " ";
}
cout << endl;
}

int main()
{
comment();
}
Output:

You might also like