Advertisement
svetoslavbozov

Brackets

Feb 15th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Text;
  4.  
  5. class Brackets
  6. {
  7.     static void Main()
  8.     {
  9.         StringBuilder text = new StringBuilder();
  10.         int n = int.Parse(Console.ReadLine());
  11.         string pad = Console.ReadLine();
  12.         while (n > 0)
  13.         {
  14.             text.Append(Console.ReadLine().Trim());
  15.             text.Append("\r\n");
  16.             n--;
  17.         }
  18.         string line = text.ToString().Replace("{", "`{`").Replace("}", "`}`").Trim();
  19.         string[] lines = line.Split(new string[] { "\r\n", "`" }, StringSplitOptions.RemoveEmptyEntries);
  20.         text.Clear();        
  21.         int counter = 0;        
  22.         for (int i = 0; i < lines.Length; i++)
  23.         {
  24.             lines[i] = Regex.Replace(lines[i], @"(\s+)", " ");        
  25.             lines[i] = lines[i].Trim();
  26.             if (lines[i]=="{")
  27.             {
  28.                 for (int j = 0; j < counter; j++)
  29.                 {
  30.                     lines[i] = lines[i].Insert(0, pad);
  31.                 }                
  32.                 counter++;
  33.                 text.Append(lines[i]);
  34.                 text.Append("\r\n");
  35.                 continue;
  36.             }
  37.             if (lines[i]=="}")
  38.             {
  39.                 for (int j = 0; j < counter- 1; j++)
  40.                 {
  41.                     lines[i] = lines[i].Insert(0, pad);
  42.                 }
  43.                 counter--;
  44.                 text.Append(lines[i]);
  45.                 text.Append("\r\n");
  46.                 continue;
  47.             }
  48.             else if (lines[i] != String.Empty)
  49.             {
  50.                 for (int j = 0; j < counter; j++)
  51.                 {
  52.                     lines[i] = lines[i].Insert(0, pad);
  53.                 }
  54.                 text.Append(lines[i]);
  55.                 text.Append("\r\n");
  56.             }            
  57.         }
  58.         string babab = text.ToString();
  59.         Console.WriteLine(babab);
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement