Advertisement
radidim

P04. Matching_Brackets (C# Shell App Paste)

Jul 5th, 2020
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1. //Disclaimer: The creator of 'C# Shell (C# Offline Compiler)' is in no way responsible for the code posted by any user.
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Collections.Generic;
  6.  
  7. namespace CSharp_Shell
  8. {
  9.  
  10.     public static class Program
  11.     {
  12.         public static void Main()
  13.         {
  14.             var input = Console.ReadLine();
  15.             var stack = new Stack<int>();
  16.             for (int i = 0; i < input.Length; i++)
  17.             {
  18.             var ch = input[i];
  19.             if (ch == '(')
  20.             {
  21.                 stack.Push(i);
  22.             }
  23.             else if (ch == ')')
  24.             {
  25.                 var startIndex = stack.Pop();
  26.                 var contents = input.Substring(
  27.                 startIndex, i - startIndex + 1);
  28.                 Console.WriteLine(contents);
  29.             }
  30.                
  31.             }
  32.            
  33.         }
  34.        
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement