TodorMitev

AreTheBracketsCorrect

Jan 25th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _03AreBracketsCorrect
  7. {
  8.     class AreBracketsCorrect
  9.     {
  10.         static int CountElements(string s, char ch)
  11.         {
  12.             int count = 0;
  13.             foreach (char item in s)
  14.             {
  15.                 if (item == ch)
  16.                 {
  17.                     count++;
  18.                 }
  19.             }
  20.             return count;
  21.         }
  22.         static int SumOfIndexes(string s, char ch)
  23.         {
  24.             int count = 0;
  25.             for (int i = 0; i < s.Length; i++)
  26.             {
  27.                 if (s[i] == ch)
  28.                 {
  29.                     count += i;
  30.                 }
  31.             }
  32.             return count;
  33.         }
  34.         static void Main(string[] args)
  35.         {
  36.             string input = Console.ReadLine();
  37.             int rightBracketsCount = CountElements(input, ')');
  38.             int leftBracketsCount = CountElements(input, '(');
  39.  
  40.             int leftBracketsIndexes = SumOfIndexes(input,'(');
  41.             int rightBracketsIndexes = SumOfIndexes(input,')');
  42.             if ((leftBracketsCount == rightBracketsCount) && (leftBracketsIndexes < rightBracketsIndexes))
  43.             {
  44.                 Console.WriteLine("The brackets in entered expression \"{0}\" are correct!", input);
  45.             }
  46.             else
  47.             {
  48.                 Console.WriteLine("The brackets in expression \"{0}\" are NOT correct!",input);
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment