Reverse a string.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str |
original string |
new string
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