比较两个单词中的字母,看看它们是否有共同的字母

发布于 2025-01-18 07:41:04 字数 493 浏览 0 评论 0原文

函数WordPass应该将两个单词/字符串作为参数,并寻找它们共同的字母。如果他们有共同的字母,则该功能将返回为true,如果不是,则返回false。

这是我尝试的:

Function wordPass(wordleAnswer As String, guess As String) As Boolean

    For i = 1 To Len(wordleAnswer)
        For j = 1 To Len(guess)
            same = False
            If Mid(guess, j, 1) = Mid(wordleAnswer, i, 1) Then
                same = True
            End If
        Next j
    Next i

    wordPass = same

End Function

The function wordPass is supposed to take two words/strings as parameters and look for letters that they have in common. If they have letters in common, the function returns True, if not, it returns False.

Here is what I tried:

Function wordPass(wordleAnswer As String, guess As String) As Boolean

    For i = 1 To Len(wordleAnswer)
        For j = 1 To Len(guess)
            same = False
            If Mid(guess, j, 1) = Mid(wordleAnswer, i, 1) Then
                same = True
            End If
        Next j
    Next i

    wordPass = same

End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2025-01-25 07:41:04

我们可以使用instr消除一个循环:

Function wordPass(wordleAnswer As String, guess As String) As Boolean
    Dim i As Long
    For i = 1 To Len(guess)
        If InStr(wordleAnswer, Mid$(guess, i, 1)) > 0 Then
            wordPass = True
            Exit Function
        End If
    Next
End Function

We can eliminate one loop using InStr:

Function wordPass(wordleAnswer As String, guess As String) As Boolean
    Dim i As Long
    For i = 1 To Len(guess)
        If InStr(wordleAnswer, Mid$(guess, i, 1)) > 0 Then
            wordPass = True
            Exit Function
        End If
    Next
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文