adjustc(3f) - [M_strings:WHITESPACE] center text
(LICENSE:PD)
pure function adjustc(string[,length])
character(len=*),intent(in) :: string
integer,intent(in),optional :: length
character(len=:),allocatable :: adjustc
Centers input text in a string of the length specified. Returns a
string of length LENGTH if LENGTH is present. Otherwise returns a
string of the length of the input string.
string input string to trim and center
length line length to center text in, optional.
adjustc centered output string
Sample Program:
program demo_adjustc
use M_strings, only : adjustc
! using length of the input string
write(*,'(a)') '================================'
write(*,'(a)')adjustc('centered string ')
write(*,'(a)')adjustc(' centered string')
write(*,'(a)')adjustc(' centered string ')
! using explicit output string length
write(*,'(a)')repeat('=',50)
write(*,'(a)')adjustc('this is a centered string',50)
write(*,'(a)')repeat('=',50)
end program demo_adjustc
Expected output
================================
centered string
centered string
centered string
==================================================
this is a centered string
==================================================
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | string |
PROCEDURE adjustc(3f) DESCRIPTION center text using implicit or explicit length VERSION 2.0, 20160711AUTHOR John S. Urban |
||
integer, | intent(in), | optional | :: | length |
pure function adjustc(string,length)
! ident_34="@(#) M_strings adjustc(3f) center text"
!>
!! PROCEDURE adjustc(3f)
!! DESCRIPTION center text using implicit or explicit length
!!##VERSION 2.0, 20160711
!! AUTHOR John S. Urban
!-----------------------------------------------------------------------------------------------------------------------------------
character(len=*),intent(in) :: string ! input string to trim and center
integer,intent(in),optional :: length ! line length to center text in
character(len=:),allocatable :: adjustc ! output string
integer :: inlen
integer :: ileft ! left edge of string if it is centered
!-----------------------------------------------------------------------------------------------------------------------------------
if(present(length))then ! optional length
inlen=length ! length will be requested length
if(inlen <= 0)then ! bad input length
inlen=len(string) ! could not use input value, fall back to length of input string
endif
else ! output length was not explicitly specified, use input string length
inlen=len(string)
endif
allocate(character(len=inlen):: adjustc) ! create output at requested length
adjustc(1:inlen)=' ' ! initialize output string to all blanks
!-----------------------------------------------------------------------------------------------------------------------------------
ileft =(inlen-len_trim(adjustl(string)))/2 ! find starting point to start input string to center it
if(ileft > 0)then ! if string will fit centered in output
adjustc(ileft+1:inlen)=adjustl(string) ! center the input text in the output string
else ! input string will not fit centered in output string
adjustc(1:inlen)=adjustl(string) ! copy as much of input to output as can
endif
end function adjustc