Advertisement
aluin

CorrectBrackets

Jan 26th, 2014
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. using System;
  2.  
  3. /*03. Write a program to check if in a given expression the brackets are put correctly.
  4. Example of correct expression: ((a+b)/5-d).
  5. Example of incorrect expression: )(a+b)). */
  6.  
  7. class CorrectBrackets
  8. {
  9. static void Main()
  10. {
  11.  
  12. //string text = "((a+b)/5-d)";
  13. Console.Write("Enter a string: ");
  14. string text = Console.ReadLine();
  15. int counter = 0;
  16. for (int i = 0; i < text.Length; i++)
  17. {
  18. if (text[i] == '(')
  19. {
  20. counter++;
  21. }
  22. else if (text[i] == ')')
  23. {
  24. counter--;
  25. }
  26. }
  27. if (counter == 0)
  28. {
  29. Console.WriteLine("The brackets are put correctly.");
  30. }
  31. else
  32. {
  33. Console.WriteLine("The brackets are NOT put correctly.");
  34. }
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement