📌  相关文章
📜  检查给定的字符串是否是注释

📅  最后修改于: 2021-09-03 03:34:12             🧑  作者: Mango

给定一个字符串S ,代表程序中的一行,任务是检查给定的字符串是否是注释。

例子:

方法:想法是检查输入的字符串是否是注释。以下是步骤:

  • 检查第一个索引(即索引 0)的值是否为 ‘/’ 然后按照以下步骤操作,否则打印“It is not a comment”。
  • 如果行 [0] == ‘/’:
    • 如果 line[1] == ‘/’,则打印“It is a single line comment”。
    • 如果 line[1] == ‘*’,则遍历字符串,如果找到任何相邻的 ‘*’ & ‘/’ 对,则打印“It is a multi-line comment”。
  • 否则,打印“这不是评论”。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the given
// string is a comment or not
void isComment(string line)
{
 
    // If two continuous slashes
    // preceeds 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] == '/') {
 
        cout << "It is a multi-line comment";
        return;
    }
 
    cout << "It is not a comment";
}
 
// Driver Code
int 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
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to check if the given
// string is a comment or not
static void isComment(String S)
{
    char line[] = S.toCharArray();
 
    // If two continuous slashes
    // preceeds the comment
    if (line[0] == '/' && line[1] == '/' &&
        line[2] != '/')
    {
        System.out.println(
            "It is a single-line comment");
        return;
    }
 
    if (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 Code
public 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
# Python3 program for the above approach
 
# Function to check if the given
# string is a comment or not
def isComment(line):
   
    # If two continuous slashes
    # preceeds 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] == '/'):
        print("It is a multi-line comment")
        return
 
    print("It is not a comment")
 
# Driver Code
if __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#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to check if the given
// string is a comment or not
static void isComment(string S)
{
    char[] line = S.ToCharArray();
 
    // If two continuous slashes
    // preceeds 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] == '/')
    {
        Console.WriteLine("It is a multi-line comment");
        return;
    }
 
    Console.WriteLine("It is not a comment");
}
 
// Driver Code
static 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


Javascript


输出:
It is a multi-line comment

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live