implicit none

integer, parameter :: nlen=1024
character(len=nlen) :: buf
integer :: i, i1, i2, lbuf, num

read(*, '(a)') buf

lbuf = len_trim(buf)

num = 0
i=0
do while( i < lbuf )
call findnextitem( buf, nlen, i, i1, i2 )
num = num + 1
write(*,*) num, buf(i1:i2)

end do



end



subroutine findnextitem( buf, nlen, i, i1, i2 )
integer ::  nlen, i, i1, i2
character(len=nlen) :: buf

 call findnotspace( buf, nlen, i )
 i1=i
 call findspace( buf, nlen, i)
 i2=i 

! write(*,*) 'item = ', buf(i1:i2)
end


subroutine findspace( buf, nlen, i )
! find next ' ' from buf(i:  )
! result will be in i
integer :: nlen
character(len=nlen) :: buf
integer :: i

 if(i<1) then
   i = 1
 end if

 do while( buf(i:i) /= ' ' .AND. i<nlen)
  i=i+1
 end do
end

subroutine findnotspace( buf, nlen, i )
! find next non-' ' from buf(i:  )
! result will be in i
integer :: nlen
character(len=nlen) :: buf
integer :: i

 if(i<1) then
   i = 1
 end if

 do while( buf(i:i) == ' ' .AND. i<nlen)
  i=i+1
 end do
end

