Mr Beanstar
Mr Beanstar

Reputation: 25

Vb.net Changing all letters in string to different 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

Upvotes: 0

Views: 1359

Answers (4)

kajvans
kajvans

Reputation: 85

you can also do it in this way

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 

here the code makes a inputbox than does +13 and after that it will type the message

Upvotes: 0

djv
djv

Reputation: 15774

This solution supports upper case letters and also punctuation

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

usage:

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

output:

original:
The Quick Brown Fox Jumps Over The Lazy Dog!
rot13(original):
Gur Dhvpx Oebja Sbk Whzcf Bire Gur Ynml Qbt!
rot13(rot13(original)):
The Quick Brown Fox Jumps Over The Lazy Dog!

Upvotes: 1

Pure
Pure

Reputation: 158

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)

Upvotes: 2

Stuart
Stuart

Reputation: 6785

If you are referring to your variable looped which you initialise to 0, then you should increment it in each iteration:

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

Upvotes: -1

Related Questions