Task[edit]
Find the index of a string (needle) in an indexable, ordered collection of strings (haystack).

Raise an exception if the needle is missing.

If there is more than one occurrence then return the smallest index to the needle.

Extra credit
Return the largest index to a needle that has multiple occurrences in the haystack.

See also
 Search a list of records

program main
 
 implicit none
 
 character(len=7),dimension(10) :: haystack = [  &
  'Zig    ',&
  'Zag    ',&
  'Wally  ',&
  'Ronald ',&
  'Bush   ',&
  'Krusty ',&
  'Charlie',&
  'Bush   ',&
  'Boz    ',&
  'Zag    ']
 
 call find_needle('Charlie')
 call find_needle('Bush')
 
 contains
 
	subroutine find_needle(needle)
	implicit none
	character(len=*),intent(in) :: needle
	integer :: i
	do i=1,size(haystack)
		if (needle==haystack(i)) then
			write(*,'(A,I4)') trim(needle)//' found at index:',i
			return
		end if
	end do
	write(*,'(A)') 'Error: '//trim(needle)//' not found.'
	end subroutine find_needle
 
 end program main