! Program Example from "Essential Fortran 90 & 95" by Loren P. Meissner
! Copyright 1996. Copying for sale requires permission from the author.
! Otherwise, distribution is permitted if these three lines are included.

! Example 6.15. Locate all occurrences of a pattern.
  program G15
    implicit none
    integer, parameter :: LINES = 5, L_S = 50
    character (len = *), parameter :: PATTERN = "the"
    integer :: Loop, Location, I, Next
    character (len = L_S), dimension(LINES) :: Metric
! start program G15
    Metric(1) = " One centimeter equals ten millimeters. "
    Metric(2) = " One decimeter equals ten centimeters. "
    Metric(3) = " One meter equals ten decimeters. "
    Metric(4) = " One thermometer equals ten meters. "
    Metric(5) = " This is the key to the metric system. "
    do Loop = 1, LINES
      Next = 0
      do I = 1, L_S
        Location = index (Metric(Loop)(Next + 1: ), PATTERN)
        if (Location == 0) then
          exit
        else
          Next = Next + Location
          write (unit = *, fmt = *) Loop, Location, Next
        end if
      end do
    end do
    stop
  end program G15
