Logo

Programming-Idioms

History of Idiom 41 > diff from v58 to v59

Edit summary for version 59 by spectrum:
[Fortran] Variable names have been changed to t and s (rather than string_t and string_s) to match the specification

Version 58

2019-09-29, 18:43:33

Version 59

2019-09-29, 19:11:41

Idiom #41 Reverse a string

Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.

Illustration

Idiom #41 Reverse a string

Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.

Illustration
Code
  character(len=:), allocatable :: string_t
  integer ::i,n

  allocate (string_t, mold=string_s)
  n = len(string_s)
  do i=0,n-1
     string_t(n-i:n-i) = string_s(i+1:i+1)
  end do
Code
  character(len=:), allocatable :: t
  integer :: i, n

  allocate (t, mold=s)
  n = len(s)
  do i = 0, n - 1
     t(n-i : n-i) = s(i+1 : i+1)
  end do