How to check if two equal-length strings are at least 50% equal in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Function Are50PercentEqual(ByVal str1 As String, ByVal str2 As String) As Boolean
        If String.IsNullOrEmpty(str1) OrElse String.IsNullOrEmpty(str2) OrElse str1.Length <> str2.Length Then
            Return False
        End If

        Dim matchingChars As Integer = 0

        For i As Integer = 0 To str1.Length - 1
            If str1(i) = str2(i) Then
                matchingChars += 1
            End If
        Next

        Return CDbl(matchingChars) / str1.Length >= 0.5
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim str1 As String = "java vb c c++ python"
        Dim str2 As String = "java vb c r rust sql"

        If Are50PercentEqual(str1, str2) Then
            Console.WriteLine("yes")
        Else
            Console.WriteLine("no")
        End If
    End Sub
End Class


 
' run:
' 
' yes
'

 



answered May 10, 2024 by avibootz

Related questions

...