How to reverse a chain of characters?

后端 未结 5 1575
野的像风
野的像风 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 19:04

    Well, of course we want to be able to do this via a constant expression, so we attempt the following:

    program reverse
       implicit none
       character(20), parameter :: input = 'Forward'
       integer i
       character(len(input)), parameter :: output = &
          transfer([(input(i:i),i=len_trim(input),1,-1)],trim(input))
       write(*,'("#",a,"#")') input, output
    end program reverse
    

    We get the same output with both gfortran and ifort:

    #Forward             #
    #drawroF#
    

    I can't see what I might be doing wrong... is it the same bug in both compilers?

提交回复
热议问题