64 lines
1.4 KiB
Text
64 lines
1.4 KiB
Text
' FB 1.05.0 Win64
|
|
|
|
Sub split (s As Const String, sepList As Const String, result() As String)
|
|
If s = "" OrElse sepList = "" Then
|
|
Redim result(0)
|
|
result(0) = s
|
|
Return
|
|
End If
|
|
Dim As Integer i, j, count = 0, empty = 0, length
|
|
Dim As Integer position(Len(s) + 1)
|
|
position(0) = 0
|
|
|
|
For i = 0 To len(s) - 1
|
|
For j = 0 to Len(sepList) - 1
|
|
If s[i] = sepList[j] Then
|
|
count += 1
|
|
position(count) = i + 1
|
|
End If
|
|
Next j
|
|
Next i
|
|
|
|
Redim result(count)
|
|
If count = 0 Then
|
|
result(0) = s
|
|
Return
|
|
End If
|
|
|
|
position(count + 1) = len(s) + 1
|
|
|
|
For i = 1 To count + 1
|
|
length = position(i) - position(i - 1) - 1
|
|
result(i - 1) = Mid(s, position(i - 1) + 1, length)
|
|
Next
|
|
End Sub
|
|
|
|
Function reverse(s As Const String) As String
|
|
If s = "" Then Return ""
|
|
Dim t As String = s
|
|
Dim length As Integer = Len(t)
|
|
For i As Integer = 0 to length\2 - 1
|
|
Swap t[i], t[length - 1 - i]
|
|
Next
|
|
Return t
|
|
End Function
|
|
|
|
Dim s As String = "rosetta code phrase reversal"
|
|
Dim a() As String
|
|
Dim sepList As String = " "
|
|
|
|
Print "Original string => "; s
|
|
Print "Reversed string => "; reverse(s)
|
|
Print "Reversed words => ";
|
|
split s, sepList, a()
|
|
For i As Integer = LBound(a) To UBound(a)
|
|
Print reverse(a(i)); " ";
|
|
Next
|
|
Print
|
|
Print "Reversed order => ";
|
|
For i As Integer = UBound(a) To LBound(a) Step -1
|
|
Print a(i); " ";
|
|
Next
|
|
Print : Print
|
|
Print "Press any key to quit"
|
|
Sleep
|