!======================================================================
   program brackets_case
   ! To read line-by-line from standard input, and count
   ! left and right parentheses(), braces{}, brackets[]

      ! Initialise counters for each type    
      integer :: lpars=0,rpars=0,lbraces=0,rbraces=0,lbracks=0,rbracks=0

      ! Declare a sufficiently long character string to hold each 
      ! line of the input file, and one to hold the ith character
      character line*200, ithc*1

      ! Start the read loop -- reading 1st 200 characters from each line
2     read(*,'(A)',end=1) line  

      ! Test each character in the input line, and keep count
      do i=1,len_trim(line)   !Inbuilt function len_trim returns length
                              !after trailing blanks removed.
                              !Less efficiently, could do i=1,200
         !ith character in line, i.e. from position i to position i is
         ithc = line(i:i) 
         select case (ithc)
         case('(')               !Left parantheses
            lpars= lpars+1
         case(')')               !Right parentheses
            rpars=rpars+1        
         case('[')               !Left brackets
            lbracks=lbracks+1 
         case(']')               !Right brackets
            rbracks=rbracks+1
         case('{')               !Left braces
            lbraces=lbraces+1
         case('}')               !Right braces
            rbraces=rbraces+1
         end select
      end do
      
      ! Read another line
      goto 2

      ! Print the respective totals
1     print 100, lpars, rpars,lbraces,rbraces,lbracks,rbracks
100   format(/I5,' ( ,  ',I5,' ) ,' &
             /I5,' { ,  ',I5,' } ,' &
             /I5,' [ ,  ',I5,' ] ,' /)
             
   end program
!======================================================================
