Vb.net将字符串中的所有字母更改为不同的字母

时间:2016-11-03 12:38:54

标签: vb.net string variables

我正在创建一些代码,它接受用户输入(这是一个字符串)并将每个字母更改为字母表中的13个字母。但我已经明白了文字的长度。但是,我不知道如何在每次将字母更改为字母表中的第13个字符时为变量添加1。

Dim looped As Integer
Dim length As Integer
Dim text As String
Dim newtext As String


Sub Main()

    Console.WriteLine("Enter some text")
    text = Console.ReadLine

    looped = 0
    text = LCase(text)
    length = Len(text)

    For counter = 1 To 25 Step 1

        text = text.Replace("a", "n")
        text = text.Replace("b", "o")
        text = text.Replace("c", "p")
        text = text.Replace("d", "q")
        text = text.Replace("e", "r")
        text = text.Replace("f", "s")
        text = text.Replace("g", "t")
        text = text.Replace("h", "u")
        text = text.Replace("i", "v")
        text = text.Replace("j", "w")
        text = text.Replace("k", "x")
        text = text.Replace("l", "y")
        text = text.Replace("m", "z")
        text = text.Replace("n", "a")
        text = text.Replace("o", "b")
        text = text.Replace("p", "c")
        text = text.Replace("q", "d")
        text = text.Replace("r", "e")
        text = text.Replace("s", "f")
        text = text.Replace("t", "g")
        text = text.Replace("u", "h")
        text = text.Replace("v", "i")
        text = text.Replace("w", "j")
        text = text.Replace("x", "k")
        text = text.Replace("y", "l")
        text = text.Replace("z", "m")

    Next

    Console.WriteLine(text)
    Console.ReadLine()

End Sub

4 个答案:

答案 0 :(得分:2)

看起来像一个简单的“加密”方法。如果你只想要小写a-z。 通过添加1,我猜你的意思是递增到下一个字符,如果你想这样做,我会使用某种字符代码。 我会这样做(快速模拟):

    Dim LetterArray As String = "abcdefghijklmnopqrstuvwxyz" 'ANYTHING you want
    Dim NewLetterArray As String = ""
    Dim LetterStep As Integer = 13 'Can be from 0 to 13 in your scenario.
    For Each CurrentLetter As Char In LetterArray
        If (Asc(CurrentLetter) + LetterStep) > Asc("z") Then
            NewLetterArray = NewLetterArray & Chr(Asc(CurrentLetter) + LetterStep - (LetterStep * 2))
        Else
            NewLetterArray = NewLetterArray & Chr(Asc(CurrentLetter) + LetterStep)
        End If
    Next
    Console.WriteLine(NewLetterArray)

答案 1 :(得分:1)

此解决方案支持大写字母和标点符号

Private Function rot13(input As String) As String
    Dim sb As New System.Text.StringBuilder()
    For Each c In input
        Dim a = Asc(c)
        Select Case a
            Case 97 To 122
                ' lower case letters a to z
                ' a - 84 = a - 97 + 13
                sb.Append(Chr(((a - 84) Mod 26) + 97))
            Case 65 To 90
                ' upper case letters A to Z
                ' a - 52 = a - 65 + 13
                sb.Append(Chr(((a - 52) Mod 26) + 65))
            Case Else
                ' all other characters, i.e. punctuation
                sb.Append(c)
        End Select
    Next c
    Return sb.ToString()
End Function

用法:

Sub Main()
    Dim input = "The Quick Brown Fox Jumps Over The Lazy Dog!"
    Console.WriteLine("original:")
    Console.WriteLine(input)

    input = rot13(input)
    Console.WriteLine("rot13(original):")
    Console.WriteLine(input)

    input = rot13(input)
    Console.WriteLine("rot13(rot13(original)):")
    Console.WriteLine(input)

    Console.ReadLine()
End Sub

输出:

  

原:   
快速的棕色狐狸跳过懒狗!   
ROT13(原始):   
Gur Dhvpx Oebja Sbk Whzcf Bire Gur Ynml Qbt!   
ROT13(ROT13(原件)):   
快速的棕色狐狸跳过懒狗!

答案 2 :(得分:0)

您也可以通过这种方式完成

set x = WScript.CreateObject("WScript.Shell")
encrypt = inputbox("type text om te versleutelen")
x.Run "%windir%\notepad"
wscript.sleep 1000
x.sendkeys encode(encrypt)

function encode(s)
For i = 1 To Len(s)
newtxt = Mid(s, i, 1)
newtxt = Chr(Asc(newtxt)+13)
coded = coded & newtxt
Next
encode = coded
End Function 

在此代码中,输入框比+13宽,然后将输入消息

答案 3 :(得分:-1)

如果您指的是初始化为0的变量looped,那么您应该在每次迭代中递增它:

Dim looped As Integer
Dim length As Integer
Dim text As String
Dim newtext As String

Sub Main()
    Console.WriteLine("Enter some text")
    text = Console.ReadLine

    looped = 0
    text = LCase(text)
    length = Len(text)

    For counter = 1 To 25 Step 1
        text = text.Replace("a", "n")
        text = text.Replace("b", "o")
        text = text.Replace("c", "p")
        text = text.Replace("d", "q")
        text = text.Replace("e", "r")
        text = text.Replace("f", "s")
        text = text.Replace("g", "t")
        text = text.Replace("h", "u")
        text = text.Replace("i", "v")
        text = text.Replace("j", "w")
        text = text.Replace("k", "x")
        text = text.Replace("l", "y")
        text = text.Replace("m", "z")
        text = text.Replace("n", "a")
        text = text.Replace("o", "b")
        text = text.Replace("p", "c")
        text = text.Replace("q", "d")
        text = text.Replace("r", "e")
        text = text.Replace("s", "f")
        text = text.Replace("t", "g")
        text = text.Replace("u", "h")
        text = text.Replace("v", "i")
        text = text.Replace("w", "j")
        text = text.Replace("x", "k")
        text = text.Replace("y", "l")
        text = text.Replace("z", "m")

        ' Increment "looped" here:
        looped = looped + 1

    Next

    Console.WriteLine(text)
    Console.ReadLine()

End Sub