How to reverse a chain of characters?

后端 未结 5 1614
野的像风
野的像风 2021-01-24 18:24

I have to create a program that reverses phrases.

For example: when I write hello guys, I want syug olleh

This is what

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-24 18:50

    @Rook's answer works for odd length of string too? Anyway here's my attempt, inspired by his answer which takes into account trailing blanks too:

        ! Reverse the string
        function reverse_string(string) result(gnirts)
    
            character(len=*) :: string
            character(len(string)) :: gnirts
            character(1) :: temp
            integer :: i, length
    
            length = len(string) 
    
            do i = 1,length
                gnirts(length-i+1:length-i+1) = string(i:i)
            end do
    
    end function reverse_string
    

提交回复
热议问题