upper(3f) - [M_strings:CASE] changes a string to uppercase (LICENSE:PD)
elemental pure function upper(str,begin,end) result (string)
character(*), intent(in) :: str
integer,optional,intent(in) :: begin,end
character(len(str)) :: string ! output string
upper(string) returns a copy of the input string with all characters
converted in the optionally specified range to uppercase, assuming
ASCII character sets are being used. If no range is specified the
entire string is converted to uppercase.
str string to convert to uppercase
begin optional starting position in "str" to begin converting to
uppercase
end optional ending position in "str" to stop converting to
uppercase
upper copy of the input string with all characters converted to
uppercase over optionally specified range.
The terms "uppercase" and "lowercase" date back to the early days of
the mechanical printing press. Individual metal alloy casts of each
needed letter, or punctuation symbol, were meticulously added to a
press block, by hand, before rolling out copies of a page. These
metal casts were stored and organized in wooden cases. The more
often needed miniscule letters were placed closer to hand, in the
lower cases of the work bench. The less often needed, capitalized,
majuscule letters, ended up in the harder to reach upper cases.
Sample program:
program demo_upper
use M_strings, only: upper
implicit none
character(len=:),allocatable :: s
s=' ABCDEFG abcdefg '
write(*,*) 'mixed-case input string is ....',s
write(*,*) 'upper-case output string is ...',upper(s)
write(*,*) 'make first character uppercase ... ',&
& upper('this is a sentence.',1,1)
write(*,'(1x,a,*(a:,"+"))') 'UPPER(3f) is elemental ==>',&
& upper(["abc","def","ghi"])
end program demo_upper
Expected output
mixed-case input string is .... ABCDEFG abcdefg
upper-case output string is ... ABCDEFG ABCDEFG
make first character uppercase ... This is a sentence.
UPPER(3f) is elemental ==>ABC+DEF+GHI
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character, | intent(in) | :: | str | |||
integer, | intent(in), | optional | :: | begin | ||
integer, | intent(in), | optional | :: | end |
elemental pure function upper(str,begin,end) result (string)
! ident_23="@(#) M_strings upper(3f) returns a trimmed uppercase string"
character(*), intent(in) :: str ! input string to convert to all uppercase
integer, intent(in), optional :: begin,end
character(len(str)) :: string ! output string that contains no miniscule letters
integer :: i ! loop counter
integer :: ibegin,iend
integer,parameter :: diff = iachar('A')-iachar('a')
string = str ! initialize output string to input string
ibegin=1
iend=len_trim(str)
if (present(begin))then
ibegin = min(max(ibegin,begin),iend)
endif
if (present(end))then
iend= max(1,min(iend,end))
endif
do concurrent (i = ibegin:iend) ! step thru each letter in the string in specified range
select case (str(i:i))
case ('a':'z') ! located miniscule letter
string(i:i) = achar(iachar(str(i:i))+diff) ! change miniscule letter to majascule
end select
enddo
end function upper