Option Explicit
Function find_yaps1(s As String) As Long
'soluzione 1: utilizzo di Mid, Left e Right
Dim i As Long, z As String, cnt As Long
s = LCase(s)
For i = 1 To Len(s) - 2
z = Mid(s, i, 3)
If Left(z, 1) = "y" And Right(z, 1) = "p" Then cnt = cnt + 1
Next
find_yaps1 = cnt
End Function
Function find_yaps2(s As String) As Long
'soluzione 1: utilizzo di Like
Dim i As Long, z As String, cnt As Long
s = LCase(s)
For i = 1 To Len(s) - 2
z = Mid(s, i, 3)
If z Like "y?p" Then cnt = cnt + 1
Next
find_yaps2 = cnt
End Function
Function find_yaps3(s As String) As Long
'soluzione 3: utilizzo delle regex (espressioni regolari)
Dim re As Object
s = LCase(s)
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "(?=(y.p))"
re.IgnoreCase = True 'ignore case
re.Global = True 'matches all occurances
find_yaps3 = re.Execute(s).Count
End Function
Function find_yaps4(s As String)
'soluzione 4: array di byte
Dim i As Long, v() As String, b() As Byte, cnt As Integer, t As Long
b() = LCase(s)
For i = 0 To UBound(b) - 4 Step LenB("A")
If b(i) = 121 And b(i + 4) = 112 Then cnt = cnt + 1 ' 121 = "y", 112 = "p"
Next
find_yaps4 = cnt
End Function
Sub test_find_yaps()
Dim s As String, m As String, i As Integer, j As Integer
Const NUM_TESTs = 4
Debug.Print "Start ---------------------------------"
For i = 1 To NUM_TESTs
s = CStr(i)
For j = 1 To 4
Select Case j
Case 1
m = "yap" 'correct: 1
Case 2
m = "yypp" 'correct: 2
Case 3
m = "yaap" 'correct: 0
Case 4
m = "ayapo" 'correct: 1
End Select
Debug.Print "Running find_yaps" & s & ": yaps in '" & m & "'... " & Run("find_yaps" & s, m)
Next j
Debug.Print "- - - - - - - - - - - - - - - - - - - -"
Next i
Debug.Print "End -----------------------------------"
End Sub |