Check if a given string is a comment or not
Last Updated :
28 Jan, 2023
Given a string S, representing a line in a program, the task is to check if the given string is a comment or not.
Types of comments in programs:
- Single Line Comment: Comments preceded by a Double Slash (‘//’)
- Multi-line Comment: Comments starting with (‘/*’) and ending with (‘*/’).
Examples:
Input: line = “/*GeeksForGeeks GeeksForGeeks*/”
Output: It is a multi-line comment
Input: line = “// GeeksForGeeks GeeksForGeeks”
Output: It is a single-line comment
Approach: The idea is to check whether the input string is a comment or not. Below are the steps:
- Check if at the first Index(i.e. index 0) the value is ‘/’ then follow below steps else print “It is not a comment”.
- If line[0] == ‘/’:
- If line[1] == ‘/’, then print “It is a single line comment”.
- If line[1] == ‘*’, then traverse the string and if any adjacent pair of ‘*’ & ‘/’ is found then print “It is a multi-line comment”.
- Otherwise, print “It is not a comment”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void isComment(string line)
{
if (line.size()>=2 && line[0] == '/' && line[1] == '/' ) {
cout << "It is a single-line comment" ;
return ;
}
if (line.size()>=4 && line[line.size() - 2] == '*'
&& line[line.size() - 1] == '/' && line[0] == '/' && line[1] == '*' ) {
cout << "It is a multi-line comment" ;
return ;
}
cout << "It is not a comment" ;
}
int main()
{
string line = "/*GeeksForGeeks GeeksForGeeks*/" ;
isComment(line);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void isComment(String S)
{
char line[] = S.toCharArray();
if (line[ 0 ] == '/' && line[ 1 ] == '/' )
{
System.out.println(
"It is a single-line comment" );
return ;
}
if (line[ 0 ]== '/' && line[ 1 ]== '*' && line[line.length - 2 ] == '*' &&
line[line.length - 1 ] == '/' )
{
System.out.println(
"It is a multi-line comment" );
return ;
}
System.out.println( "It is not a comment" );
}
public static void main(String[] args)
{
String line = "/*GeeksForGeeks GeeksForGeeks*/" ;
isComment(line);
}
}
|
Python3
def isComment(line):
if (line[ 0 ] = = '/' and line[ 1 ] = = '/' and line[ 2 ] ! = '/' ):
print ( "It is a single-line comment" )
return
if (line[ len (line) - 2 ] = = '*' and line[ len (line) - 1 ] = = '/' and line[ 0 ] = = '/' and line[ 1 ] = = '*' ):
print ( "It is a multi-line comment" )
return
print ( "It is not a comment" )
if __name__ = = '__main__' :
line = "/*GeeksForGeeks GeeksForGeeks*/"
isComment(line)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void isComment( string S)
{
char [] line = S.ToCharArray();
if (line[0] == '/' && line[1] == '/' &&
line[2] != '/' )
{
Console.WriteLine( "It is a single-line comment" );
return ;
}
if (line[line.Length - 2] == '*' &&
line[line.Length - 1] == '/' && line[0] == '/' && line[1] == '*' )
{
Console.WriteLine( "It is a multi-line comment" );
return ;
}
Console.WriteLine( "It is not a comment" );
}
static public void Main()
{
string line = "/*GeeksForGeeks GeeksForGeeks*/" ;
isComment(line);
}
}
|
Javascript
<script>
function isComment( line)
{
var regex = new RegExp( "//.*" , );
var rex = regex.test(line);
if (rex){
document.write( "It is a single-line comment" );
return ;
}
var regexMul = new RegExp( "/\*.*?\*/" , );
var rexmul = regexMul.test(line);
if (rexmul)
{
document.write( "It is a multi-line comment" );
return ;
}
document.write( "It is not a comment" );
}
var line = "/*GeeksForGeeks GeeksForGeeks*/" ;
isComment(line);
</script>
|
Output
It is a multi-line comment
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...