2016-11-03 47 views
0

我创建了一些需要用户输入(这是一个字符串)并将每个字母更改为字母表中13个字母形式的代码。但我已经明白了文字的长度。但是我不知道如何在每次将一个字母改成字母表中的第13个字母时将变量加1。Vb.net将字符串中的所有字母更改为不同的字母

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 
+0

您需要遍历文本并分别替换每个字符。你可能会发现这段代码很有用:'Convert.ToInt32(ch)' – Neal

+2

1)这里有一点问题。说你的文字是“a”。所以它被替换为“n”。然后将“n”替换为“a”。 2)提示:搜索“ROT13”。 –

+0

[简单的rot13编码器在vb.net]可能的重复(http://stackoverflow.com/questions/13016727/simple-rot13-encoder-in-vb-net) –

回答

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) 
+0

谢谢你的帮助 –

+0

if函数中的代码块如何工作 –

+0

对不起,没有解释。随着你递增13,并且a-z字母表中只有26个字符,if函数检查你是否要用你的步骤(当前字母+13)检查ASCII字符代码“z”。如果它是以步骤* 2倒退的话,你会得到你需要的角色。 如果您在if语句中标记位置以便在visual studio中进行调试,这应该会让您更好地理解它的工作原理。 – Pure

-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 
+0

它试过了,但它只改变了第五个字母在alpahbet中的第13个字母的字符串 –

+0

它不清楚你在这里使用的是“loop”...(?) - 你知道在你的'text'变量中,你用“n “,然后再次将”n“替换回”a“.... – Stuart

+0

我的意思是删除循环变量。我试图让一个代码制作人能够把每一封信都变成第13封信,并将它变成allaphbet –

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(original):
Gur Dhvpx Oebja Sbk Whzcf Bire Gur Ynml Qbt!
rot13(rot13(original)):
快速布朗狐狸跳过懒狗!

相关问题