You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
count_char -- Count the number of times a single character c appears in a character variable s
pos_char -- Returns an integer array of the positions where c appears in s
replace -- Replace character a with b in s
compress -- Replace 2 or more consecutive occurrences of a character in s with a single occurrence. This is useful for replacing multiple spaces with a single space.
upper_case
lower_case
Here is an implementation of count_char.
module string_funcs_mod
implicit none
private
public :: count_char
contains
elemental function count_char(s, c) result(ncount)
! count the number of times character c appears in string s
character(len=*), intent(in) :: s
character, intent(in) :: c
integer :: ncount, i, len_trim_s
len_trim_s = len_trim(s)
ncount = 0
do i = 1, len_trim_s
if (s(i:i) == c) ncount = ncount + 1
end do
if (c == ' ') ncount = ncount + len(s) - len_trim_s
end function count_char
end module string_funcs_mod
!
program xstring_funcs
use string_funcs_mod
implicit none
print "(*(1x,i0))", count_char("hello world ", ["l", "a", " "])
end program xstring_funcs
output: 3 0 2
The text was updated successfully, but these errors were encountered:
A few string functions I would like are
count_char
-- Count the number of times a single characterc
appears in a character variables
pos_char
-- Returns an integer array of the positions wherec
appears ins
replace
-- Replace charactera
withb
ins
compress
-- Replace 2 or more consecutive occurrences of a character ins
with a single occurrence. This is useful for replacing multiple spaces with a single space.upper_case
lower_case
Here is an implementation of
count_char
.output:
3 0 2
The text was updated successfully, but these errors were encountered: