重新排列字母(VB)

时间:2011-11-16 18:10:51

标签: vb.net key alphabet

我想基本上用关键字infront重新排列字母表,因为我正在做一个简单的替代密码。我有点弄清楚这样做的逻辑,但我有点卡在编码方面。

我想要的是:

Dim key = "keyword"

    For i = 0 to 26

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

    Next


'output: keywordabcfghijlmnpqstuvxz

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:3)

我认为循环使用键比循环使用字母更清晰:

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)

答案 2 :(得分:1)

以下是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循环,并删除当前的char(如果已存在)。如果删除了char,请记住将for循环移回一个char。