Reverse String in Fortran

Published on 15 October 2025 (Updated: 15 October 2025)

Welcome to the Reverse String in Fortran page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

program reverse_string
   implicit none
   character(len=:), allocatable :: arg, rev
   integer :: i, n, error

   if (command_argument_count() < 1) then
      print '("")'
      stop
   end if

   ! Get length of argument
   call get_command_argument(1, length=n, status=error)
   if (error /= 0 .or. n == 0) then
      print '("")'
      stop
   end if

   ! Allocate and read argument
   allocate(character(len=n) :: arg)
   call get_command_argument(1, arg, status=error)
   if (error /= 0) then
      print '("")'
      deallocate(arg)
      stop 0
   end if

   ! Allocate and create reversed string
   allocate(character(len=n) :: rev)
   do i = 1, n
      rev(i:i) = arg(n-i+1:n-i+1)
   end do

   print '(A)', rev
end program reverse_string

Reverse String in Fortran was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.