reverse Function

public pure function reverse(str) result(newstr)

Reverse a string.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str

original string

Return Value character(len=:), allocatable

new string


Called by

proc~~reverse~~CalledByGraph proc~reverse reverse proc~rchop rchop proc~rchop->proc~reverse

Source Code

    pure function reverse(str) result(newstr)

    character(len=*),intent(in) :: str  !! original string
    character(len=:),allocatable :: newstr !! new string
    integer :: i, j !! counter
    integer :: n !! length of `str`

    n = len(str)
    allocate(character(len=n) :: newstr)
    if (n==0) then
        newstr = ''
    else
        j = 0
        do i = n, 1, -1
            j = j + 1
            newstr(j:j) = str(i:i)
        end do
    end if

    end function reverse