by VB-Kung-Fu (19 Submissions)
Category: String Manipulation
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Thu 9th October 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Reverses the order of words in a string.
Dim n As Long, Character As String, ReversedOrder As String, temp As String
n = Len(StringToUse)
Do
'select a character only if n > 1 or Mid() wont work
If n <> 0 Then
Character = Mid(StringToUse, n, 1)
End If
If Character = " " Or n = 0 Then
'if the selected character is space then reverse the contents of the temp string
'since we have part of the original string backwards
temp = StrReverse(temp)
'put the the reversed string fragment into the return string
ReversedOrder = ReversedOrder & " " & temp
'clear the temporary storage area so that another word can be stored
temp = ""
Else
' if the character selected was not a space then store it
temp = temp & Character
End If
n = n - 1
Loop While n > -1
'here the LTrim removes the leading space that is introduced the first
'time the loop runs
ReverseWordOrder = LTrim(ReversedOrder)
End Function