Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _03AreBracketsCorrect
- {
- class AreBracketsCorrect
- {
- static int CountElements(string s, char ch)
- {
- int count = 0;
- foreach (char item in s)
- {
- if (item == ch)
- {
- count++;
- }
- }
- return count;
- }
- static int SumOfIndexes(string s, char ch)
- {
- int count = 0;
- for (int i = 0; i < s.Length; i++)
- {
- if (s[i] == ch)
- {
- count += i;
- }
- }
- return count;
- }
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- int rightBracketsCount = CountElements(input, ')');
- int leftBracketsCount = CountElements(input, '(');
- int leftBracketsIndexes = SumOfIndexes(input,'(');
- int rightBracketsIndexes = SumOfIndexes(input,')');
- if ((leftBracketsCount == rightBracketsCount) && (leftBracketsIndexes < rightBracketsIndexes))
- {
- Console.WriteLine("The brackets in entered expression \"{0}\" are correct!", input);
- }
- else
- {
- Console.WriteLine("The brackets in expression \"{0}\" are NOT correct!",input);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment