Advertisement
Vapio

task9

Jul 27th, 2022 (edited)
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. using System;
  2.  
  3. public class Program
  4. {
  5.     public static void Main(string[] args)
  6.     {
  7.  
  8.         char bracketStart = '(';
  9.         char bracketEnd = ')';
  10.  
  11.         Console.Write("Input Brackets : ");
  12.         string brackets = Console.ReadLine();
  13.  
  14.         int bracketsDepth = 0;
  15.         int bracketsDepthMax = 0;
  16.         bool isWrong = false;
  17.  
  18.         foreach(char bracket in brackets)
  19.         {
  20.             if(bracket == bracketStart)
  21.             {
  22.                 ++bracketsDepth;
  23.             }
  24.             else if(bracket == bracketEnd)
  25.             {
  26.                 if(bracketsDepth > bracketsDepthMax)
  27.                     bracketsDepthMax = bracketsDepth;
  28.                 --bracketsDepth;
  29.             }
  30.                
  31.             if(bracketsDepth < 0)
  32.             {
  33.                 isWrong = true;
  34.                 break;
  35.             }
  36.         }
  37.  
  38.         if(isWrong)
  39.             Console.WriteLine("String with brackets is incorrect.");
  40.         else
  41.             Console.WriteLine("String with brackets is correct. Max depth of brackets is : " + bracketsDepthMax);
  42.  
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement