Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Program
- {
- public static bool checkBracketsX(string text)
- {
- Stack<char> check = new Stack<char>();
- foreach (char ch in text)
- {
- if (ch == '(' || ch == '[' || ch == '{')
- {
- check.Push(ch);
- }
- else if (ch == ')' || ch == ']' || ch == '}')
- {
- if (check.Count == 0)
- {
- return false;
- }
- else
- {
- char skoba = check.Pop();
- switch (ch)
- {
- case ')':
- if (skoba != '(')
- return false;
- break;
- case ']':
- if (skoba != '[')
- return false;
- break;
- case '}':
- if (skoba != '{')
- return false;
- break;
- }
- }
- }
- }
- if (check.Count == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static bool checkBrackets(string text)
- {
- Stack<char> check = new Stack<char>();
- foreach (char ch in text)
- {
- if (ch == '(')
- {
- check.Push('(');
- }
- else if (ch == ')')
- {
- if (check.Count == 0)
- {
- return false;
- }
- else
- {
- check.Pop();
- }
- }
- }
- if (check.Count == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static void Main()
- {
- Console.Write("Enter text: ");
- string text = Console.ReadLine();
- bool flag = true;
- Stack<char> check = new Stack<char>();
- foreach (char ch in text)
- {
- if (ch == '(')
- {
- check.Push('(');
- }
- else if (ch == ')')
- {
- if (check.Count == 0)
- {
- flag = false;
- break;
- }
- else
- {
- check.Pop();
- }
- }
- }
- if (check.Count != 0)
- {
- flag = false;
- }
- if (flag)
- {
- Console.WriteLine("Correct!");
- }
- else
- {
- Console.WriteLine("Incorrect!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement