Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- class Program
- {
- // Write a program to check if in a given expression the brackets are put correctly.
- // Example of correct expression: ((a+b)/5-d).
- // Example of incorrect expression: )(a+b)).
- static void Main()
- {
- string expression1 = "((a+b)/5-d)";
- CheckingBreckets(expression1);
- string expression2 = ")(a+b))";
- CheckingBreckets(expression2);
- }
- public static void CheckingBreckets(string expression)
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < expression.Length; i++)
- {
- if(expression[i]=='('||expression[i]==')')
- sb.Append(expression [i]);
- }
- bool isSymetric = true;
- int length = sb.Length;
- if (length >= 2)
- {
- for (int i = 0; i < length / 2; i++)
- {
- if (sb[i] == '(' && sb[length - i - 1] == ')')
- {
- isSymetric = true;
- continue;
- }
- else
- {
- isSymetric = false;
- break;
- }
- }
- Console.WriteLine(isSymetric);
- }
- else
- {
- isSymetric = false;
- Console.WriteLine(isSymetric);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment