fork download
  1. program CheckBrackets1;
  2. const
  3. n = 40;
  4. var
  5. st: array[1..n] of char;
  6. i, count, len: integer;
  7. begin
  8. writeln('Enter a string (up to 40 chars):');
  9. i := 1;
  10. repeat
  11. read(st[i]);
  12. i := i + 1;
  13. until (eoln) or (i > n);
  14. len := i - 1;
  15.  
  16. count := 0;
  17. for i := 1 to len do
  18. begin
  19. if st[i] = '(' then
  20. count := count + 1
  21. else if st[i] = ')' then
  22. count := count - 1;
  23.  
  24. if count < 0 then break;
  25. end;
  26.  
  27. if count = 0 then
  28. writeln('Brackets are balanced.')
  29. else
  30. writeln('Brackets are NOT balanced.');
  31. end.
Success #stdin #stdout 0s 5316KB
stdin
5
stdout
Enter a string (up to 40 chars):
Brackets are balanced.