Advertisement
jhei13

Parenthesis Checker

Oct 3rd, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1.  
  2.         s:
  3.             string input;
  4.             int a = 0;
  5.             int Tcount = 0;
  6.  
  7.             Stack ch = new Stack();
  8.             Console.WriteLine("Input Anything :");
  9.             input = Console.ReadLine();
  10.  
  11.             foreach (char c in input)
  12.             {
  13.                 if (c.Equals('('))
  14.                 {
  15.                     ch.Push('(');
  16.                     Tcount += 1;
  17.                    
  18.                 }
  19.                 else if (c.Equals(')'))
  20.                 {
  21.                     try
  22.                     {
  23.                         ch.Pop();
  24.                         a++;
  25.                     }
  26.                     catch(Exception)
  27.                     {
  28.                         Console.WriteLine("( is expected");
  29.                         Console.ReadLine();
  30.                         goto s;
  31.                     }
  32.                 }
  33.             }
  34.  
  35.  
  36.             if (ch.Count == 0)
  37.             {
  38.                 Console.WriteLine(Tcount + " valid ");
  39.                
  40.             }
  41.             else
  42.             {
  43.                 Console.WriteLine( ") is expected");
  44.             }
  45.             goto s;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement