Check if a given string is a comment or not

19 Apr, 2022
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”.
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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to check if the given// string is a comment or notvoid isComment(string line){     // If two continuous slashes    // precedes the comment    if (line[0] == '/' && line[1] == '/'        && line[2] != '/') {         cout << "It is a single-line comment";        return;    }     if (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";} // Driver Codeint main(){    // Given string    string line = "/*GeeksForGeeks GeeksForGeeks*/";     // Function call to check whether then    // given string is a comment or not    isComment(line);     return 0;}
// Java program for the above approachimport java.io.*;import java.lang.*;import java.util.*; class GFG{ // Function to check if the given// string is a comment or notstatic void isComment(String S){    char line[] = S.toCharArray();     // If two continuous slashes    // precedes the comment    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");} // Driver Codepublic static void main(String[] args){         // Given string    String line = "/*GeeksForGeeks GeeksForGeeks*/";     // Function call to check whether then    // given string is a comment or not    isComment(line);}} // This code is contributed by Kingash
# Python3 program for the above approach # Function to check if the given# string is a comment or notdef isComment(line):       # If two continuous slashes    # precedes the comment    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") # Driver Codeif __name__ == '__main__':       # Given string    line = "/*GeeksForGeeks GeeksForGeeks*/"         # Function call to check whether then    # given string is a comment or not    isComment(line)         # This code is contributed by ipg2016107.
// C# program for the above approachusing System;using System.Collections.Generic; class GFG{     // Function to check if the given// string is a comment or notstatic void isComment(string S){    char[] line = S.ToCharArray();     // If two continuous slashes    // precedes the comment    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");} // Driver Codestatic public void Main(){         // Given string    string line = "/*GeeksForGeeks GeeksForGeeks*/";     // Function call to check whether then    // given string is a comment or not    isComment(line);}} // This code is contributed by splevel62
<script> // JavaScript program for the above approach   // Function to check if the given// string is a comment or notfunction isComment( line){      // If two continuous slashes    // precedes the comment    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");}// Driver Code // Given stringvar line = "/*GeeksForGeeks GeeksForGeeks*/"; // Function call to check whether then// given string is a comment or notisComment(line);           </script>
It is a multi-line comment
Time Complexity: O(N)Auxiliary Space: O(1)
 
ipg2016107
Kingash
splevel62
jana_sayantan
koratnayan03
ac2000choudhary
khushboogoyal499
sagartomar9927
Technical Scripter 2020
Searching
Strings
Technical Scripter
Searching
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Search, insert and delete in a sorted array
Given a sorted and rotated array, find if there is a pair with a given sum
Allocate minimum number of pages
Find common elements in three sorted arrays
Find whether an array is subset of another array
Write a program to reverse an array or string
Reverse a string in Java
C++ Data Types
Write a program to print all permutations of a given string
Check for Balanced Brackets in an expression (well-formedness) using Stack