ajetrumpet
07-05-2009, 08:16 AM
Here is some code that will scramble a word or string using a dynamic array. In others words, scrambling or randomizing "on the fly" using functions...
Declarations:Public WrdArray() As StringScramble Function:Function Scramble(x As String, y As String)
Dim i As Integer, j As Integer
j = Len(y)
If j < 2 Then
ReDim Preserve WrdArray(UBound(WrdArray) + 1)
WrdArray(UBound(WrdArray)) = x & y
Else
For i = 1 To j
Call Scramble(x + Mid(y, i, 1), _
left(y, i - 1) + right(y, j - i))
Next i
End If
End FunctionInitialize Array:Function InitiateArray()
ReDim Preserve WrdArray(0)
End FunctionYou can scramble strings by calling both functions:Function ShowScrambledWord()
Randomize
Call InitializeArray
Call Scramble("", "STRING TO SCRAMBLE HERE")
Debug.Print WrdArray(Int((UBound(WrdArray) - 0 + 1) * Rnd + 0))
End Function
Declarations:Public WrdArray() As StringScramble Function:Function Scramble(x As String, y As String)
Dim i As Integer, j As Integer
j = Len(y)
If j < 2 Then
ReDim Preserve WrdArray(UBound(WrdArray) + 1)
WrdArray(UBound(WrdArray)) = x & y
Else
For i = 1 To j
Call Scramble(x + Mid(y, i, 1), _
left(y, i - 1) + right(y, j - i))
Next i
End If
End FunctionInitialize Array:Function InitiateArray()
ReDim Preserve WrdArray(0)
End FunctionYou can scramble strings by calling both functions:Function ShowScrambledWord()
Randomize
Call InitializeArray
Call Scramble("", "STRING TO SCRAMBLE HERE")
Debug.Print WrdArray(Int((UBound(WrdArray) - 0 + 1) * Rnd + 0))
End Function