Last active
May 16, 2023 16:42
-
-
Save Strelok78/4afa73406849d0c06ede10fa28bc66bf to your computer and use it in GitHub Desktop.
A string of the characters '(' and ')' is given. It is determined whether it is a valid parenthesis expression. The maximum depth of nesting of brackets is determined.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyCode | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const char OpenBrace = '('; | |
const char CloseBrace = ')'; | |
int countBracesLevel = 0; | |
int countBracesCorrectness = 0; | |
string enteredBraces; | |
enteredBraces = Console.ReadLine(); | |
foreach (char brace in enteredBraces) | |
{ | |
switch (brace) | |
{ | |
case OpenBrace: | |
countBracesCorrectness++; | |
break; | |
case CloseBrace: | |
countBracesCorrectness--; | |
break; | |
default: | |
break; | |
} | |
if (countBracesCorrectness < 0) | |
{ | |
break; | |
} | |
countBracesLevel = countBracesCorrectness > countBracesLevel ? countBracesCorrectness : countBracesLevel; | |
} | |
if (countBracesCorrectness == 0) | |
{ | |
Console.WriteLine($"Line is correct!"); | |
Console.WriteLine($"Max braces level: {countBracesLevel}"); | |
} | |
else | |
{ | |
Console.WriteLine("Line is incorrect!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment