Reverse words in a string: Difference between revisions

Content added Content deleted
Line 2,300: Line 2,300:
{{works with|ksh}}
{{works with|ksh}}
Same as above, except change <lang bash>read -a</lang> to <lang bash>read -A</lang>
Same as above, except change <lang bash>read -a</lang> to <lang bash>read -A</lang>

=={{header|VBScript}}==
<lang vb>
Option Explicit

Dim objFSO, objInFile, objOutFile
Dim srcDir, line

Set objFSO = CreateObject("Scripting.FileSystemObject")

srcDir = objFSO.GetParentFolderName(WScript.ScriptFullName) & "\"

Set objInFile = objFSO.OpenTextFile(srcDir & "In.txt",1,False,0)

Set objOutFile = objFSO.OpenTextFile(srcDir & "Out.txt",2,True,0)

Do Until objInFile.AtEndOfStream
line = objInFile.ReadLine
If line = "" Then
objOutFile.WriteLine ""
Else
objOutFile.WriteLine Reverse_String(line)
End If
Loop

Function Reverse_String(s)
Dim arr, i
arr = Split(s," ")
For i = UBound(arr) To LBound(arr) Step -1
If arr(i) <> "" Then
If i = UBound(arr) Then
Reverse_String = Reverse_String & arr(i)
Else
Reverse_String = Reverse_String & " " & arr(i)
End If
End If
Next
End Function

objInFile.Close
objOutFile.Close
Set objFSO = Nothing
</lang>

{{Out}}
Output written to a file.
<pre>
------------ Fire and Ice ----------

Some say the world will end in fire,
Some say in ice.
From what I've tasted of desire
I hold with those who favor fire.

... last paragraph elided ...

----------------------- Robert Frost
</pre>


=={{header|zkl}}==
=={{header|zkl}}==