Reverse words in a string: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 2,823: | Line 2,823: | ||
----------------------- Robert Frost</pre> |
----------------------- Robert Frost</pre> |
||
Another version (tested with FPC 3.2.2) |
|||
<syntaxhighlight lang="pascal"> |
|||
program reverse_words; |
|||
{$mode objfpc}{$h+} |
|||
uses |
|||
SysUtils; |
|||
function Reverse(a: TStringArray): TStringArray; |
|||
var |
|||
I, J: SizeInt; |
|||
t: Pointer; |
|||
begin |
|||
I := 0; |
|||
J := High(a); |
|||
while I < J do begin |
|||
t := Pointer(a[I]); |
|||
Pointer(a[I]) := Pointer(a[J]); |
|||
Pointer(a[J]) := t; |
|||
Inc(I); |
|||
Dec(J); |
|||
end; |
|||
Result := a; |
|||
end; |
|||
const |
|||
Input = |
|||
'---------- Ice and Fire -----------' + LineEnding + |
|||
'' + LineEnding + |
|||
'fire, in end will world the say Some' + LineEnding + |
|||
'ice. in say Some' + LineEnding + |
|||
'desire of tasted I''ve what From' + LineEnding + |
|||
'fire. favor who those with hold I' + LineEnding + |
|||
'' + LineEnding + |
|||
'... elided paragraph last ...' + LineEnding + |
|||
'' + LineEnding + |
|||
'Frost Robert -----------------------' + LineEnding; |
|||
var |
|||
Line: string; |
|||
begin |
|||
for Line in Input.Split([LineEnding], TStringSplitOptions.ExcludeLastEmpty) do |
|||
WriteLn(string.Join(' ', Reverse(Line.Split([' '])))); |
|||
end. |
|||
</syntaxhighlight> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |