Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
898 views
in Technique[技术] by (71.8m points)

fortran - How to reverse a chain of characters?

I have to create a program that reverses phrases.

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

This is what I have for the moment but I don't know how to reverse the characters in the board phraseT:

program reversing    
implicit none    
character(len=20),dimension(1:20)::phraseT    
character(len=20)::phrase,reverse    
integer::i,x


write(*,*)"write a phrase  :"    
read(*,'(a)')phrase    
x=len(trim(phrase))    
nomT(1:20)=" " 

do i=1,x    
nomT(i:i)=trim(phrase(i:i))    
end do

write(*,*)nomT    
end program reversing
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a shorter way to do it:

forall (i=1:len(string)) string(i:i) = string(len(string)-i+1:len(string)-i+1)

It even saves a handful of bytes ! I'm not sure I'd suggest this was more intelligent though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...