How to reverse a chain of characters?

后端 未结 5 1589
野的像风
野的像风 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:53

    One simple way to do it ...

    character(80) :: string = "Whatttt's up doc?"
    character     :: temp
    integer       :: i, length
    
    write (*,*) string    ! Writes it out proper.
      length = len_trim(string) ! ignores trailing blanks. 
                                ! use len(string) to reverse those as well
      do i = 1, length/2
         temp = string(i:i)
         string(i:i) = string(length+1-i:length+1-i)
         string(length+1-i:length+1-i) = temp
      end do
    write(*,*) string     ! Writes it out backwards.
    end
    

    Others will come up with more intelligent ones (and shorter) for sure.

提交回复
热议问题