Text Data Processing
Character Scan
Count Matches
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.
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
text ← banana band
8text = 'banana band'9target_index = 1values this stepbanana bandtexttarget_index ← 1
8text = 'banana band'9target_index = 110select case (target_index)values this step1target_indexmatched ← case (1)
9target_index = 110select case (target_index)11 case (1)values this stepcase (1)matched1target_indextarget ← a
11case (1)12 target = 'a'13case (2)values this stepatargethits ← 0
17end select18hits = 019do i = 1, len_trim(text)values this step0hitshits ← 4
19do i = 1, len_trim(text)20 if (text(i:i) == target) hits = hits + 121end dovalues this step4hitsbanana bandtextatargetprint '(A, 1X, I0)', target, hits
21 end do22 print '(A, 1X, I0)', target, hits23end program character_scan_demooutputa 4values this stepatarget4hits
text ← banana band
8text = 'banana band'9target_index = 2values this stepbanana bandtexttarget_index ← 2
8text = 'banana band'9target_index = 210select case (target_index)values this step2target_indexmatched ← case (2)
9target_index = 210select case (target_index)11 case (1)values this stepcase (2)matched2target_indextarget ← n
13case (2)14 target = 'n'15case defaultvalues this stepntargethits ← 0
17end select18hits = 019do i = 1, len_trim(text)values this step0hitshits ← 3
19do i = 1, len_trim(text)20 if (text(i:i) == target) hits = hits + 121end dovalues this step3hitsbanana bandtextntargetprint '(A, 1X, I0)', target, hits
21 end do22 print '(A, 1X, I0)', target, hits23end program character_scan_demooutputn 3values this stepntarget3hits
text ← banana band
8text = 'banana band'9target_index = 3values this stepbanana bandtexttarget_index ← 3
8text = 'banana band'9target_index = 310select case (target_index)values this step3target_indexmatched ← case default
9target_index = 310select case (target_index)11 case (1)values this stepcase defaultmatched3target_indextarget ← b
15 case default16 target = 'b'17end selectvalues this stepbtargethits ← 0
17end select18hits = 019do i = 1, len_trim(text)values this step0hitshits ← 2
19do i = 1, len_trim(text)20 if (text(i:i) == target) hits = hits + 121end dovalues this step2hitsbanana bandtextbtargetprint '(A, 1X, I0)', target, hits
21 end do22 print '(A, 1X, I0)', target, hits23end program character_scan_demooutputb 2values 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.