A loop over character slices can count simple text features while keeping every step explicit.

Program

Play the program to choose which character to count in the sample text.

target_index
character_scan.f90
Replay: real traced execution (multi-file project)
program character_scan_demo
    implicit none
    integer :: target_index
    integer :: i, hits
    character(len=12) :: text
    character(len=1) :: target

    text = 'banana band'
    target_index = 1
    select case (target_index)
        case (1)
            target = 'a'
        case (2)
            target = 'n'
        case default
            target = 'b'
    end select
    hits = 0
    do i = 1, len_trim(text)
        if (text(i:i) == target) hits = hits + 1
    end do
    print '(A, 1X, I0)', target, hits
end program character_scan_demo
program character_scan_demo
    implicit none
    integer :: target_index
    integer :: i, hits
    character(len=12) :: text
    character(len=1) :: target

    text = 'banana band'
    target_index = 2
    select case (target_index)
        case (1)
            target = 'a'
        case (2)
            target = 'n'
        case default
            target = 'b'
    end select
    hits = 0
    do i = 1, len_trim(text)
        if (text(i:i) == target) hits = hits + 1
    end do
    print '(A, 1X, I0)', target, hits
end program character_scan_demo
program character_scan_demo
    implicit none
    integer :: target_index
    integer :: i, hits
    character(len=12) :: text
    character(len=1) :: target

    text = 'banana band'
    target_index = 3
    select case (target_index)
        case (1)
            target = 'a'
        case (2)
            target = 'n'
        case default
            target = 'b'
    end select
    hits = 0
    do i = 1, len_trim(text)
        if (text(i:i) == target) hits = hits + 1
    end do
    print '(A, 1X, I0)', target, hits
end program character_scan_demo
  1. text ← banana band

    8text = 'banana band'9target_index = 1
    values this stepbanana bandtext
  2. target_index ← 1

    8text = 'banana band'9target_index = 110select case (target_index)
    values this step1target_index
  3. matched ← case (1)

    9target_index = 110select case (target_index)11    case (1)
    values this stepcase (1)matched1target_index
  4. target ← a

    11case (1)12    target = 'a'13case (2)
    values this stepatarget
  5. hits ← 0

    17end select18hits = 019do i = 1, len_trim(text)
    values this step0hits
  6. hits ← 4

    19do i = 1, len_trim(text)20    if (text(i:i) == target) hits = hits + 121end do
    values this step4hitsbanana bandtextatarget
  7. print '(A, 1X, I0)', target, hits

    21    end do22    print '(A, 1X, I0)', target, hits23end program character_scan_demo
    outputa 4
    values this stepatarget4hits
  1. text ← banana band

    8text = 'banana band'9target_index = 2
    values this stepbanana bandtext
  2. target_index ← 2

    8text = 'banana band'9target_index = 210select case (target_index)
    values this step2target_index
  3. matched ← case (2)

    9target_index = 210select case (target_index)11    case (1)
    values this stepcase (2)matched2target_index
  4. target ← n

    13case (2)14    target = 'n'15case default
    values this stepntarget
  5. hits ← 0

    17end select18hits = 019do i = 1, len_trim(text)
    values this step0hits
  6. hits ← 3

    19do i = 1, len_trim(text)20    if (text(i:i) == target) hits = hits + 121end do
    values this step3hitsbanana bandtextntarget
  7. print '(A, 1X, I0)', target, hits

    21    end do22    print '(A, 1X, I0)', target, hits23end program character_scan_demo
    outputn 3
    values this stepntarget3hits
  1. text ← banana band

    8text = 'banana band'9target_index = 3
    values this stepbanana bandtext
  2. target_index ← 3

    8text = 'banana band'9target_index = 310select case (target_index)
    values this step3target_index
  3. matched ← case default

    9target_index = 310select case (target_index)11    case (1)
    values this stepcase defaultmatched3target_index
  4. target ← b

    15    case default16        target = 'b'17end select
    values this stepbtarget
  5. hits ← 0

    17end select18hits = 019do i = 1, len_trim(text)
    values this step0hits
  6. hits ← 2

    19do i = 1, len_trim(text)20    if (text(i:i) == target) hits = hits + 121end do
    values this step2hitsbanana bandtextbtarget
  7. print '(A, 1X, I0)', target, hits

    21    end do22    print '(A, 1X, I0)', target, hits23end program character_scan_demo
    outputb 2
    values this stepbtarget2hits
character slice `text(i:i)` reads one character at position `i`.
counter loop The loop increments `hits` only when the selected character matches.
len_trim `len_trim(text)` keeps the scan inside the visible text.