How to resolve the algorithm Count occurrences of a substring step by step in the FreeBASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Count occurrences of a substring step by step in the FreeBASIC programming language
Table of Contents
Problem Statement
Create a function, or show a built-in function, to count the number of non-overlapping occurrences of a substring inside a string. The function should take two arguments:
It should return an integer count. The matching should yield the highest number of non-overlapping matches. In general, this essentially means matching from left-to-right or right-to-left (see proof on talk page).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Count occurrences of a substring step by step in the FreeBASIC programming language
Source code in the freebasic programming language
' FB 1.05.0 Win64
Function countSubstring(s As String, search As String) As Integer
If s = "" OrElse search = "" Then Return 0
Dim As Integer count = 0, length = Len(search)
For i As Integer = 1 To Len(s)
If Mid(s, i, length) = Search Then
count += 1
i += length - 1
End If
Next
Return count
End Function
Print countSubstring("the three truths","th")
Print countSubstring("ababababab","abab")
Print countSubString("zzzzzzzzzzzzzzz", "z")
Print
Print "Press any key to quit"
Sleep
You may also check:How to resolve the algorithm Narcissist step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Bitwise operations step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Curzon numbers step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Elliptic Curve Digital Signature Algorithm step by step in the FreeBASIC programming language