internal class Program { static void Main(string[] args) { char openParenthesis = '('; char closedParenthesis = ')'; int countParenthesis = 0; int maxCountParenthesis = 0; Console.Write($"Введите строку из скобок '{openParenthesis}' и '{closedParenthesis}' - "); string userInput = Console.ReadLine(); foreach (char parenthesis in userInput) { if (parenthesis == openParenthesis) { countParenthesis++; if (countParenthesis > maxCountParenthesis) maxCountParenthesis = countParenthesis; } else { countParenthesis--; if (countParenthesis < 0) break; } } if (countParenthesis == 0) { Console.WriteLine($"строка корректная и максимум глубины равняется {maxCountParenthesis}"); } else { Console.WriteLine("Некорректная строка."); } Console.ReadKey(); } }