Reverse words in a string: Difference between revisions
Content added Content deleted
Line 695: | Line 695: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Compiled using G95 on x86 system running Puppy Linux |
|||
<lang fortran>\L<G> <U>=@{$2} $1</lang> |
|||
Fortran syntax is mostly Fortran 77 |
|||
<lang fortran> |
|||
character*40 words |
|||
character*40 reversed |
|||
logical inblank |
|||
ierr=0 |
|||
read (5,fmt="(a)",iostat=ierr)words |
|||
do while (ierr.eq.0) |
|||
inblank=.true. |
|||
ipos=1 |
|||
do i=40,1,-1 |
|||
if(words(i:i).ne.' '.and.inblank) then |
|||
last=i |
|||
inblank=.false. |
|||
end if |
|||
if(.not.inblank.and.words(i:i).eq.' ') then |
|||
reversed(ipos:ipos+last-i)=words(i+1:last) |
|||
ipos=ipos+last-i+1 |
|||
inblank=.true. |
|||
end if |
|||
if(.not.inblank.and.i.eq.1) then |
|||
reversed(ipos:ipos+last-1)=words(1:last) |
|||
ipos=ipos+last |
|||
end if |
|||
end do |
|||
print *,words,'=> ',reversed(1:ipos-1) |
|||
read (5,fmt="(a)",iostat=ierr)words |
|||
end do |
|||
end |
|||
<lang> |
|||
Output from comand: cat frostPoem.txt | reverse |
|||
<pre> |
|||
---------- Ice and Fire ----------- => ----------- Fire and Ice ---------- |
|||
=> |
|||
fire, in end will world the say Some => Some say the world will end in fire, |
|||
ice. in say Some => Some say in ice. |
|||
desire of tasted I've what From => From what I've tasted of desire |
|||
fire. favor who those with hold I => I hold with those who favor fire. |
|||
=> |
|||
... elided paragraph last ... => ... last paragraph elided ... |
|||
=> |
|||
Frost Robert ----------------------- => ----------------------- Robert Frost |
|||
</pre> |
|||
=={{header|Gema}}== |
=={{header|Gema}}== |
||
<lang gema>\L<G> <U>=@{$2} $1</lang> |
<lang gema>\L<G> <U>=@{$2} $1</lang> |