MOHDCD
MOHDCD
EXPERIMENT-3
Theory: The aim is to determine whether or not a given string, S, which represents a line in a program, is a
comment.
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[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;
EXPERIMENT-3
Output:
Name: MOHD SHAHJAHAN CSE-B Roll No: 2100320100104
EXPERIMENT-3
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:
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] == '/'))
{
int main()
{
comment();
}
Output: