challenge コメントの削除

ソースコードからコメント部分を削除するプログラム decomment を書いてください.
すくなくとも,decomment を記述したのと同じ言語で書かれているソースコードが
扱えるようにしてください.



Posted feedbacks - C#

オプションでエンコーディングを指定できます。 (例: decomment -utf-8 )

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Text;

class Program
{
  static void Main(string[] args)
  {
    if (args.Length > 0 && args[0][0] == '-')
    {
      Console.InputEncoding =
          Encoding.GetEncoding(args[0].Substring(1));
    }
    string line = null;
    bool block = false;
    while ((line = Console.ReadLine()) != null)
    {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < line.Length; i++)
      {
        if (block)
        {
          if (line[i] == '*' && i < line.Length - 1 && line[i + 1] == '/')
          {
            block = false;
            i++;
          }
          continue;
        }
        if (line[i] == '/' && i < line.Length - 1)
        {
          if (line[i + 1] == '/')
          {
            break;
          }
          if (line[i + 1] == '*')
          {
            block = true;
            i++;
            continue;
          }
        }
        sb.Append(line[i]);
      }
      Console.WriteLine(sb);
    }
  }
}

Index

Feed

Other

Link

Pathtraq

loading...