count_token Function

public pure function count_token(str, token) result(count)

This function counts the occurrence of a spcified character(token) in a given string.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str
character(len=1), intent(in) :: token

Return Value integer


Source Code

   pure function count_token(str, token) result(count)
      implicit none
      character(*), intent(in) :: str     ! Input string to be searched.
      character(1), intent(in) :: token   ! Character to be counted in the input string.

      integer :: count     ! Result: number of occurrences of the `token`.
      integer :: i         ! Loop index variable.
      integer :: siz       ! Length of the input string.

      ! Initialize the count to zero.
      count = 0

      ! Get the length of the input string.
      siz = len(str)

      ! Loop through each character in the string.
      do i = 1, siz
         ! If the current character matches the `token`, increment the `count`.
         if (str(i:i) == token) count = count + 1
      end do
   end function count_token