Check if a given string is a comment or not


In computer programming, comments are text written with the source code but ignored by the compiler or interpreter. They are used to provide readability of code by describing the code and its functionality for someone who is reading the code other than a compiler or interpreter. They are not executed and do not affect the functionality of the overall program, they are just for programmer guidance. Each programming language has a different syntax to represent comments. Here are a few examples −

  • C/C++ − In C or C++, single-lined comments begin with ‘//’ and multi-liner comments are enclosed in ‘/*’ and ‘*/’.

// Single-lined comment
/* Multi-
lined
comment */
  • Java − In Java, single-lined comments begin with ‘//’ and multi-liner comments are enclosed in ‘/*’ and ‘*/’.

// Single-lined comment
/* Multi-
lined
comment */
  • Python − In Python, single-lined comments begin with # and triple quotes can be used for writing a multi-lined string that is not assigned a variable.

# Single-lined comment
'''
Multi-
lined
comment
'''
  • Javascript − In Javascript, single-lined comments begin with ‘//’ and multi-liner comments are enclosed in ‘/*’ and ‘*/’.

// Single-lined comment
/* Multi-
lined
comment */

Problem Statement

Given a string. Check if the string is a comment in C++ or not.

Sample Example 1

Input: ‘/hello world */’
Output: FALSE

Explanation − The input string neither starts with // nor is enclosed by /* and */. So the string is not a comment in C++.

Sample Example 2

Input: ‘//hello world */’
Output: TRUE

Explanation − The input string starts with //. Thus, it is a comment in C++.

Approach 1: Single-lined comments

Single-lined comments span only one line and can be identified in C++ by ‘//’ preceding the comment i.e. single-lined comments in C++ always start with ‘//’. Thus, for checking for single-lined comments in the given string, we take the first two characters in the string and check if they are ‘//’, then the string can be called a single-lined comment irrespective of what follows the ‘//’ characters.

Pseudocode

procedure isComment (string)
   if string[0] == ‘/’ and string[1] == ‘/’
      ans = TRUE
   end if
   ans = FALSE
end procedure

Example

Below is the C++ Implementation of the above approach.

In the following program, we check the first two characters of the input string to check for single-lined comments.

#include <iostream>
#include <string>
using namespace std;
// Function to check if the string is a single-lined comment
bool isComment(string str){    

   // Single-lined comment if first two characters are '/'
   if (str[0] == '/' && str[1] == '/') {
      return true;
   }
   return false;
}
int main(){
   string input = "/hello world */";
   cout << "Input String: "<< input << endl;
   if (isComment(input)) {
      cout << "The input string is a comment." << endl;
   } else {
      cout << "The input string is not a comment." << endl;
   }
   return 0;
}

Output

When you compile the above program, it will produce the following outotu −

Input String: /hello world */
The input string is not a comment.

Time Complexity − O(1) as in the isComment() function we check the first two characters using the index that takes constant time.

Space Complexity − O(1) as no additional space is used.

Approach 2: Multi-lined Comments

Multi-lined comments span multiple lines and can be identified in C++ as enclosed by ‘/*’ and ‘*/’. Thus, for checking for multi-lined comments in the given string, we take the first two characters in the string and check if they are ‘/*’ and check the last two characters and check if they are ‘*/’, then the string can be called a multi-lined comment irrespective of what lies between ‘/*’ and ‘*/’.

Input: ‘/* hello world */’
Output: TRUE

Explanation − The input string is enclosed in ‘/*’ and ’*/’, thus it is a string in C++.

Pseudocode

procedure isComment (string)
   n = string.length
   if (string[0] == ‘/’ and string[1] == ‘*’) and (string[n - 1] == ‘/’ and string[n - 2] == ‘*’)
      ans = TRUE
   end if
   ans = FALSE
end procedure

Example: C++ Implementation

In the following program, we check that the input string is enclosed by ‘/*’ and ‘*/’.

#include <iostream>
#include <string>
using namespace std;

// Function to check for multi-lined comment
bool isComment(string str){
   int n = str.length();
   
   // Multi-lined comment if first two characters are '/*' and last two characters are '*/'
   if ((str[0] == '/' && str[1] == '*') && (str[n-1] == '/' && str[n-2] == '*')) {
      return true;
   }
   return false;
}
int main(){
   string input = "/* hello world */";
   cout << "Input String: " << input << endl;
   if (isComment(input)) {
      cout << "The input string is a comment." << endl;
   } else {
      cout << "The input string is not a comment." << endl;
   }
   return 0;
}

Output

When you compile the above program, it will produce the following output −

Input String: /* hello world */
The input string is a comment.

Time Complexity − O(1) as in the isComment() function we check the first two and last two characters using the index that takes constant time.

Space Complexity − O(1) as no additional space is used.

Approach 3: Single-lined and Multi-lined Comments

For a given string, to find if the comment is a single-lined or multi-line comment, we combine the above two approaches where a single-lined comment starts with’//’ and a multi-lined comment is enclosed in ‘/*’ and ‘*/’.

Input: ‘/&* hello world */’
Output: Not a comment

Pseudocode

procedure isComment (string)
   n = string.length
   if string[0] == ‘/’ and string[1] == ‘/’
      ans = 1
   else if (string[0] == ‘/’ and string[1] == ‘*’) and (string[n - 1] == ‘/’ and string[n - 2] == ‘*’)
      ans = 2
   end if
   ans = 0
end procedure

Example: C++ Implementation

In the following program, given a string, we check if it is a single-lined, multi-lined comment or not a comment at all

#include <iostream>
#include <string>
using namespace std;

// FUunction to check if the input string is comment
int isComment(string str){
   int n = str.length();
   
   // SIngle-lined comment if starting with '//'
   if (str[0] == '/' && str[1] == '/') {
      return 1;
   } 
   
   // Multi-lined comment if enclosed in '/*' and '*/'
   else if ((str[0] == '/' && str[1] == '*') && (str[n-1] == '/' && str[n-2] == '*')) {
      return 2;
   }
   
   // Not a comment
   return 0;
}
int main(){
   string input = "// hello world */";
   cout << "Input String: " << input << endl;
   if (isComment(input) == 1) {
      cout << "The input string is a single-lined comment." << endl;
   } 
   else if (isComment(input) == 2) {
      cout << "The input string is a multi-lined comment." << endl;
   } 
   else {
      cout << "The input string is not a comment." << endl;
   }
   return 0;
}

Output

Input String: // hello world */
The input string is a single-lined comment.

Time Complexity − O(1) as in the isComment() function we check for comment specifiers using the index that takes constant time.

Space Complexity − O(1) as no additional space is used.

Conclusion

In conclusion, different programming languages have different syntaxes to represent comments. In the above approaches, comments in C or C++ have been identified with time and space complexity of O(1).

Updated on: 25-Jul-2023

767 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements