当我正在做一个简单的替换密码时,我想基本上用关键字 infront 重新排列字母表。我已经弄清楚了这样做的逻辑,但我在编码方面有点卡住了。
我想要的是这样的:
Dim key = "keyword"
For i = 0 to 26
'insert keyword and put in A to Z after without duplicating characters
Next
'output: keywordabcfghijlmnpqstuvxz
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
我认为遍历键比遍历字母表更干净:
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)
这是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 循环向后移动一个字符。