How to resolve the algorithm Multisplit step by step in the FreeBASIC programming language
How to resolve the algorithm Multisplit step by step in the FreeBASIC programming language
Table of Contents
Problem Statement
It is often necessary to split a string into pieces based on several different (potentially multi-character) separator strings, while still retaining the information about which separators were present in the input. This is particularly useful when doing small parsing tasks. The task is to write code to demonstrate this. The function (or procedure or method, as appropriate) should take an input string and an ordered collection of separators. The order of the separators is significant: The delimiter order represents priority in matching, with the first defined delimiter having the highest priority. In cases where there would be an ambiguity as to which separator to use at a particular point (e.g., because one separator is a prefix of another) the separator with the highest priority should be used. Delimiters can be reused and the output from the function should be an ordered sequence of substrings. Test your code using the input string “a!===b=!=c” and the separators “==”, “!=” and “=”. For these inputs the string should be parsed as "a" (!=) "" (==) "b" (=) "" (!=) "c", where matched delimiters are shown in parentheses, and separated strings are quoted, so our resulting output is "a", empty string, "b", empty string, "c". Note that the quotation marks are shown for clarity and do not form part of the output. Extra Credit: provide information that indicates which separator was matched at each separation point and where in the input string that separator was matched.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Multisplit step by step in the FreeBASIC programming language
Source code in the freebasic programming language
' FB 1.05.0 Win64
Sub Split(s As String, sepList() As String, result() As String, removeEmpty As Boolean = False, showSepInfo As Boolean = False)
If s = "" OrElse UBound(sepList) = -1 Then
Redim result(0)
result(0) = s
Return
End If
Dim As Integer i = 0, j, count = 0, empty = 0, length
Dim As Integer position(len(s) + 1)
Dim As Integer sepIndex(1 To len(s))
Dim As Integer sepLength(len(s))
position(0) = 0 : sepLength(0) = 1
While i < Len(s)
For j = lbound(sepList) To ubound(sepList)
length = len(sepList(j))
If length = 0 Then Continue For '' ignore blank separators
If mid(s, i + 1, length) = sepList(j) Then
count += 1
position(count) = i + 1
sepIndex(count) = j
sepLength(count) = length
i += length - 1
Exit For
End If
Next j
i += 1
Wend
Redim result(count)
If count = 0 Then
If showSepInfo Then
Print "No delimiters were found" : Print
End If
result(0) = s
Return
End If
position(count + 1) = len(s) + 1
For i = 1 To count + 1
length = position(i) - position(i - 1) - sepLength(i - 1)
result(i - 1 - empty) = Mid(s, position(i - 1) + sepLength(i - 1), length)
If removeEmpty AndAlso cbool(length = 0) Then empty += 1
Next
If empty > 0 Then Redim Preserve result(count - empty)
If showSepInfo Then
Print "The 1-based indices of the delimiters found are : "
Print
For x As Integer = 1 To count
Print "At index"; position(x), sepList(sepIndex(x))
Next
Print
End If
End Sub
Dim s As String = "a!===b=!=c"
Print "The string to be split is : "; s
Print
Dim a() As String '' to hold results
Dim b(1 To 3) As String = {"==", "!=", "="} '' separators to be used in order of priority (highest first)
split s, b(), a(), False, True '' show separator info
Print "The sub-strings are : "
Print
For i As integer = 0 To ubound(a)
Print Using "##"; i + 1;
Print " : "; a(i)
Next
Print
Print "Press any key to quit"
Sleep
You may also check:How to resolve the algorithm Truncate a file step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Sleeping Beauty problem step by step in the J programming language
You may also check:How to resolve the algorithm Main step of GOST 28147-89 step by step in the Java programming language
You may also check:How to resolve the algorithm Descending primes step by step in the 11l programming language
You may also check:How to resolve the algorithm Polymorphism step by step in the ActionScript programming language