重新排列字母表 (VB)

发布于 2024-12-15 16:19:21 字数 277 浏览 2 评论 0原文

当我正在做一个简单的替换密码时,我基本上想用前面的关键字重新排列字母表。我已经弄清楚了这样做的逻辑,但我有点卡在编码方面。

我想要的是这样的:

Dim key = "keyword"

    For i = 0 to 26

    'insert keyword and put in A to Z after without duplicating characters

    Next


'output: keywordabcfghijlmnpqstuvxz

I wanted to basically rearrange the alphabet with a keyword infront as I am doing a simple substitutional cipher. I have kind of worked out the logic of doing that but I am a bit stuck on the coding side.

What I wanted was something like:

Dim key = "keyword"

    For i = 0 to 26

    'insert keyword and put in A to Z after without duplicating characters

    Next


'output: keywordabcfghijlmnpqstuvxz

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

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

发布评论

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

评论(3

纵性 2024-12-22 16:19:21

我认为循环遍历密钥比循环遍历字母表更干净:

Dim key as string = "keyword"

Dim alphabet As new StringBuilder("abcdefghijklmnopqrstuvwxyz")

for each c As Char in key
    alphabet.Replace(c.ToString(), Nothing)
next

return key & alphabet.ToString()

或者稍微更有效地更改替换行,如下所示,以避免在每次迭代时扫描字母表的所有 26 个字母:

    alphabet.Replace(c.ToString(), Nothing, 0, 1)

I think looping through the key is cleaner than looping through the alphabet:

Dim key as string = "keyword"

Dim alphabet As new StringBuilder("abcdefghijklmnopqrstuvwxyz")

for each c As Char in key
    alphabet.Replace(c.ToString(), Nothing)
next

return key & alphabet.ToString()

or slightly more efficient change the replace line as follows to avoid scanning all 26 letters of the alphabet on each iteration:

    alphabet.Replace(c.ToString(), Nothing, 0, 1)
只有一腔孤勇 2024-12-22 16:19:21
Public Function Rearrange(keyword As String) As String
    Dim cipher As New StringBuilder(26)
    cipher.Append(keyword)
    For c As Char = "a"C To "z"C
        If Not keyword.Contains(c) Then
            cipher.Append(c)
        End If
    Next
    Return cipher.ToString()
End Function
Public Function Rearrange(keyword As String) As String
    Dim cipher As New StringBuilder(26)
    cipher.Append(keyword)
    For c As Char = "a"C To "z"C
        If Not keyword.Contains(c) Then
            cipher.Append(c)
        End If
    Next
    Return cipher.ToString()
End Function
余罪 2024-12-22 16:19:21

这是 vb 中的解决方案:

Public Function words(key As String) As String
    Dim ret As String
    ret = key
    key = key.ToLower()
    For c As Char = "a"c To "z"c
        If Not key.Contains(c) Then
            ret = ret + c.ToString
        End If
    Next

    Return ret
End Function

如果您想检查键的唯一字符,那么您可以只对键的字符运行 for 循环,并删除当前字符(如果已经存在)。如果删除该字符,请记住将 for 循环移回一个字符。

Here is the solution in vb:

Public Function words(key As String) As String
    Dim ret As String
    ret = key
    key = key.ToLower()
    For c As Char = "a"c To "z"c
        If Not key.Contains(c) Then
            ret = ret + c.ToString
        End If
    Next

    Return ret
End Function

If you want to check for unique char for the key, then you can just run a for loop through the characters of the key and remove the current char if already exist. Remember to move the for loop one char back if you delete the char.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文