Experiment No - 3 (A) Aim: Write A Program To Identify Single and Multiline Comments
Experiment No - 3 (A) Aim: Write A Program To Identify Single and Multiline Comments
Experiment No – 3(a)
Aim: Write a Program to identify single and multiline comments
Theory:
#include <bits/stdc++.h>
return;
return;
int main(){
string s;
getline(cin,s);
comment(s);
return 0;
}
PIYUSH SHARMA CSE-B 2100320100117
Experiment No – 3(a)
Aim: Write a Program to remove single and multiline comments
Theory:
Given a C++ program as input, remove the comments from it. ‘source’ is a vector where the i-th line of the
source code is the source[i]. This represents the result of splitting the source code string by the newline
character \n. In C++, we can create two types of comments, i.e., Line Comments, Block Comments.
The string ‘\’ denotes the line comment, which means the string next to it on the right will be ignored by
the program.
The string ‘\* and *\’ is a multiline comment representing the string starting from ‘\* till the *\’ will be
ignored.
The first useful comment takes precedence over others: if the string // occurs in a block comment, it is
ignored. Similarly, if the string /* occurs in a line or block comment, it is also ignored. If a certain line of
code is empty after removing comments, you must not output that line − each string in the answer list will
be non-empty.
Code:
#include <iostream>
int n = s.length();
string ans;
else if (multi_c == true && s[i] == '*' && s[i+1] == '/') multi_c = false, i++;
return ans; }
int main(){
string s;
getline(cin,s);
cout<<Rcomments(s)<<endl;
PIYUSH SHARMA CSE-B 2100320100117
getline(cin,s);
cout<<Rcomments(s);
return 0;
Output: