September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
61
Task/Search-a-list/FreeBASIC/search-a-list.freebasic
Normal file
61
Task/Search-a-list/FreeBASIC/search-a-list.freebasic
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
' FB 1.05.0 Win64
|
||||
' Works FB 1.05.0 Linux Mint 64
|
||||
|
||||
Function tryFindString(s() As String, search As String, ByRef index As Integer) As Boolean
|
||||
Dim length As Integer = UBound(s) - LBound(s) + 1
|
||||
If length = 0 Then
|
||||
index = LBound(s) - 1 '' outside array
|
||||
Return False
|
||||
End If
|
||||
For i As Integer = LBound(s) To UBound(s)
|
||||
If s(i) = search Then
|
||||
index = i '' first occurrence
|
||||
Return True
|
||||
End If
|
||||
Next
|
||||
index = LBound(s) - 1 '' outside array
|
||||
Return False
|
||||
End Function
|
||||
|
||||
Function tryFindLastString(s() As String, search As String, ByRef index As Integer) As Boolean
|
||||
Dim length As Integer = UBound(s) - LBound(s) + 1
|
||||
If length = 0 Then
|
||||
index = LBound(s) - 1 '' outside array
|
||||
Return False
|
||||
End If
|
||||
Dim maxIndex As Integer = LBound(s) - 1 '' outside array
|
||||
For i As Integer = LBound(s) To UBound(s)
|
||||
If s(i) = search Then
|
||||
maxIndex = i
|
||||
End If
|
||||
Next
|
||||
If maxIndex > LBound(s) - 1 Then
|
||||
index = maxIndex '' last occurrence
|
||||
Return True
|
||||
Else
|
||||
Return False
|
||||
End If
|
||||
End Function
|
||||
|
||||
Dim haystack(1 To 9) As String = {"Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Bozo"}
|
||||
Dim needle(1 To 4) As String = {"Zag", "Krusty", "Washington", "Bush"}
|
||||
|
||||
Dim As Integer index
|
||||
Dim As Boolean found
|
||||
For i As Integer = 1 To 4
|
||||
found = tryFindString(haystack(), needle(i), index)
|
||||
If found Then
|
||||
Print needle(i); " found first at index"; index
|
||||
Else
|
||||
Print needle(i); " is not present"
|
||||
End If
|
||||
Next
|
||||
found = tryFindLastString(haystack(), needle(4), index)
|
||||
If found Then
|
||||
Print needle(4); " found last at index"; index
|
||||
Else
|
||||
Print needle(4); " is not present"
|
||||
End If
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
Loading…
Add table
Add a link
Reference in a new issue