Mr Beanstar Mr Beanstar - 1 month ago 6
Vb.net Question

Vb.net Changing all letters in string to diferent letters

I am creating a bit of code that takes the users input(which is a string) and changes every letter to the 13 letter form it in the alphabet. But I have made it to see the length on the text. But I don't know how to add 1 to a variable every time it changes a letter to the 13th on along in the alphabet.

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


I gave done this so far could anyone help.

Answer

Looks like a simple "encryption" method. If you only want lower case a-z. By adding 1, I guess you mean incrementing to the next character, if you want to do this I would use a character code of some sort. I would do it this way (quick mock up):

    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)