-
Jan 7th, 2017, 04:37 PM
#1
Thread Starter
New Member
String Scrambler
blue Code:
'opstring is a string from console.readline
Function StringScramble() As String
Dim output As String
For Each character As String In opstring
If character = "a" Then
output = output + "cpw"
End If
If character = "b" Then
output = output + "pwl"
End If
If character = "c" Then
output = output + "lwb"
End If
If character = "d" Then
output = output + "CV"
End If
If character = "e" Then
output = output + "VC"
End If
If character = "g" Then
output = output + "VnB"
End If
If character = "h" Then
output = output + "Bgw"
End If
If character = "i" Then
output = output + "epN"
End If
If character = "j" Then
output = output + "qWt"
End If
If character = "k" Then
output = output + "dlP"
End If
If character = "l" Then
output = output + "poT"
End If
If character = "m" Then
output = output + "erV"
End If
If character = "n" Then
output = output + "poV"
End If
If character = "o" Then
output = output + "zXN"
End If
If character = "p" Then
output = output + "oIe"
End If
If character = "q" Then
output = output + "QxG"
End If
If character = "r" Then
output = output + "lDJ"
End If
If character = "s" Then
output = output + "RyM"
End If
If character = "t" Then
output = output + "kSf"
End If
If character = "u" Then
output = output + "ioN"
End If
If character = "v" Then
output = output + "xQI"
End If
If character = "w" Then
output = output + "fZE"
End If
If character = "x" Then
output = output + "tRe"
End If
If character = "y" Then
output = output + "aAD"
End If
If character = "z" Then
output = output + "jjK"
End If
Next
Return output
End Function
-
Jan 8th, 2017, 04:11 PM
#2
Re: String Scrambler
Originally Posted by kickinthatcan
blue Code:
'opstring is a string from console.readline
Function StringScramble() As String
Dim output As String
For Each character As String In opstring
If character = "a" Then
output = output + "cpw"
End If
If character = "b" Then
output = output + "pwl"
End If
If character = "c" Then
output = output + "lwb"
End If
If character = "d" Then
output = output + "CV"
End If
If character = "e" Then
output = output + "VC"
End If
If character = "g" Then
output = output + "VnB"
End If
If character = "h" Then
output = output + "Bgw"
End If
If character = "i" Then
output = output + "epN"
End If
If character = "j" Then
output = output + "qWt"
End If
If character = "k" Then
output = output + "dlP"
End If
If character = "l" Then
output = output + "poT"
End If
If character = "m" Then
output = output + "erV"
End If
If character = "n" Then
output = output + "poV"
End If
If character = "o" Then
output = output + "zXN"
End If
If character = "p" Then
output = output + "oIe"
End If
If character = "q" Then
output = output + "QxG"
End If
If character = "r" Then
output = output + "lDJ"
End If
If character = "s" Then
output = output + "RyM"
End If
If character = "t" Then
output = output + "kSf"
End If
If character = "u" Then
output = output + "ioN"
End If
If character = "v" Then
output = output + "xQI"
End If
If character = "w" Then
output = output + "fZE"
End If
If character = "x" Then
output = output + "tRe"
End If
If character = "y" Then
output = output + "aAD"
End If
If character = "z" Then
output = output + "jjK"
End If
Next
Return output
End Function
Instead of all of those If/End if lines why not wrap them up into single lines for each character? Or just use a Select Case on them?
Also I notice your function relies on an outside variable (a variable not passed in) which is fine so long as the scope of that variable never changes and you only use this in the one place, though it's easily modifiable to use a variable passed in, then it can be moved around or shared to other projects or developers easier. Lastly I noticed that you only have this encoding lower case letters, which is fine if that's all you're going to do, but if needed a single line of code could be added to ensure all the letters are lower case in the string. Here's an example with all three of these points put in:
vb.net Code:
Function StringScramble(str As String) As String
Dim output As String = String.Empty
'Make case insensitive
str = str.ToLower()
For Each character As Char In str
Select Case character
Case "a"c : output &= "cpw"
Case "b"c : output &= "pwl"
Case "c"c : output &= "lwb"
Case "d"c : output &= "CV"
Case "e"c : output &= "VC"
Case "g"c : output &= "VnB"
Case "h"c : output &= "Bgw"
Case "i"c : output &= "epN"
Case "j"c : output &= "qWt"
Case "k"c : output &= "dlP"
Case "l"c : output &= "poT"
Case "m"c : output &= "erV"
Case "n"c : output &= "poV"
Case "o"c : output &= "zXN"
Case "p"c : output &= "oIe"
Case "q"c : output &= "QxG"
Case "r"c : output &= "lDJ"
Case "s"c : output &= "RyM"
Case "t"c : output &= "kSf"
Case "u"c : output &= "ioN"
Case "v"c : output &= "xQI"
Case "w"c : output &= "fZE"
Case "x"c : output &= "tRe"
Case "y"c : output &= "aAD"
Case "z"c : output &= "jjK"
End Select
Next character
Return output
End Function
I've also shortcut the output = output & "xxx" stuff with the &= operator.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|