Advertisement
Assi

13. Strings and Text Processing 3. Brackets

Jan 24th, 2013
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1. using System;
  2.  
  3. class Brackets
  4. {
  5.     static void Main()
  6.     {
  7.         string expr = ")((2+3)/2)(";
  8.         CheckIsTheExpresionIsCorrect(expr);
  9.  
  10.     }
  11.     static void CheckIsTheExpresionIsCorrect(string expr)
  12.     {
  13.         char[] exprCharArr = expr.ToCharArray();
  14.         int bracket = 0;
  15.  
  16.         bool res = true;
  17.        
  18.         for (int i = 0; i < exprCharArr.Length; i++)
  19.         {
  20.  
  21.             if (exprCharArr[i] == '(')
  22.             {
  23.                 bracket++;
  24.             }
  25.             else if (exprCharArr[i] == ')')
  26.             {
  27.                 bracket--;
  28.             }
  29.             if (bracket<0)
  30.             {
  31.                 break;
  32.             }
  33.         }
  34.         if (bracket == 0)
  35.         {
  36.             Console.WriteLine("The expresion is correct!");
  37.         }
  38.         else
  39.         {
  40.             Console.WriteLine("The expresion is NOT correct!");
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement