Results 1 to 2 of 2

Thread: String Scrambler

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    8

    String Scrambler

    blue Code:
    1. 'opstring is a string from console.readline
    2.   Function StringScramble() As String
    3.         Dim output As String
    4.  
    5.         For Each character As String In opstring
    6.             If character = "a" Then
    7.                 output = output + "cpw"
    8.             End If
    9.             If character = "b" Then
    10.                 output = output + "pwl"
    11.             End If
    12.             If character = "c" Then
    13.                 output = output + "lwb"
    14.  
    15.             End If
    16.             If character = "d" Then
    17.                 output = output + "CV"
    18.             End If
    19.             If character = "e" Then
    20.                 output = output + "VC"
    21.             End If
    22.             If character = "g" Then
    23.                 output = output + "VnB"
    24.             End If
    25.             If character = "h" Then
    26.                 output = output + "Bgw"
    27.  
    28.             End If
    29.             If character = "i" Then
    30.                 output = output + "epN"
    31.             End If
    32.             If character = "j" Then
    33.                 output = output + "qWt"
    34.             End If
    35.             If character = "k" Then
    36.                 output = output + "dlP"
    37.             End If
    38.             If character = "l" Then
    39.                 output = output + "poT"
    40.             End If
    41.             If character = "m" Then
    42.                 output = output + "erV"
    43.             End If
    44.             If character = "n" Then
    45.                 output = output + "poV"
    46.             End If
    47.             If character = "o" Then
    48.                 output = output + "zXN"
    49.             End If
    50.             If character = "p" Then
    51.                 output = output + "oIe"
    52.             End If
    53.             If character = "q" Then
    54.                 output = output + "QxG"
    55.             End If
    56.             If character = "r" Then
    57.                 output = output + "lDJ"
    58.             End If
    59.             If character = "s" Then
    60.                 output = output + "RyM"
    61.             End If
    62.             If character = "t" Then
    63.                 output = output + "kSf"
    64.             End If
    65.             If character = "u" Then
    66.                 output = output + "ioN"
    67.             End If
    68.             If character = "v" Then
    69.                 output = output + "xQI"
    70.             End If
    71.             If character = "w" Then
    72.                 output = output + "fZE"
    73.  
    74.             End If
    75.             If character = "x" Then
    76.                 output = output + "tRe"
    77.             End If
    78.             If character = "y" Then
    79.                 output = output + "aAD"
    80.             End If
    81.             If character = "z" Then
    82.                 output = output + "jjK"
    83.             End If
    84.  
    85.         Next
    86.  
    87.         Return output
    88.     End Function

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: String Scrambler

    Quote Originally Posted by kickinthatcan View Post
    blue Code:
    1. 'opstring is a string from console.readline
    2.   Function StringScramble() As String
    3.         Dim output As String
    4.  
    5.         For Each character As String In opstring
    6.             If character = "a" Then
    7.                 output = output + "cpw"
    8.             End If
    9.             If character = "b" Then
    10.                 output = output + "pwl"
    11.             End If
    12.             If character = "c" Then
    13.                 output = output + "lwb"
    14.  
    15.             End If
    16.             If character = "d" Then
    17.                 output = output + "CV"
    18.             End If
    19.             If character = "e" Then
    20.                 output = output + "VC"
    21.             End If
    22.             If character = "g" Then
    23.                 output = output + "VnB"
    24.             End If
    25.             If character = "h" Then
    26.                 output = output + "Bgw"
    27.  
    28.             End If
    29.             If character = "i" Then
    30.                 output = output + "epN"
    31.             End If
    32.             If character = "j" Then
    33.                 output = output + "qWt"
    34.             End If
    35.             If character = "k" Then
    36.                 output = output + "dlP"
    37.             End If
    38.             If character = "l" Then
    39.                 output = output + "poT"
    40.             End If
    41.             If character = "m" Then
    42.                 output = output + "erV"
    43.             End If
    44.             If character = "n" Then
    45.                 output = output + "poV"
    46.             End If
    47.             If character = "o" Then
    48.                 output = output + "zXN"
    49.             End If
    50.             If character = "p" Then
    51.                 output = output + "oIe"
    52.             End If
    53.             If character = "q" Then
    54.                 output = output + "QxG"
    55.             End If
    56.             If character = "r" Then
    57.                 output = output + "lDJ"
    58.             End If
    59.             If character = "s" Then
    60.                 output = output + "RyM"
    61.             End If
    62.             If character = "t" Then
    63.                 output = output + "kSf"
    64.             End If
    65.             If character = "u" Then
    66.                 output = output + "ioN"
    67.             End If
    68.             If character = "v" Then
    69.                 output = output + "xQI"
    70.             End If
    71.             If character = "w" Then
    72.                 output = output + "fZE"
    73.  
    74.             End If
    75.             If character = "x" Then
    76.                 output = output + "tRe"
    77.             End If
    78.             If character = "y" Then
    79.                 output = output + "aAD"
    80.             End If
    81.             If character = "z" Then
    82.                 output = output + "jjK"
    83.             End If
    84.  
    85.         Next
    86.  
    87.         Return output
    88.     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:
    1. Function StringScramble(str As String) As String
    2.     Dim output As String = String.Empty
    3.  
    4.     'Make case insensitive
    5.     str = str.ToLower()
    6.  
    7.     For Each character As Char In str
    8.         Select Case character
    9.             Case "a"c : output &= "cpw"
    10.             Case "b"c : output &= "pwl"
    11.             Case "c"c : output &= "lwb"
    12.             Case "d"c : output &= "CV"
    13.             Case "e"c : output &= "VC"
    14.             Case "g"c : output &= "VnB"
    15.             Case "h"c : output &= "Bgw"
    16.             Case "i"c : output &= "epN"
    17.             Case "j"c : output &= "qWt"
    18.             Case "k"c : output &= "dlP"
    19.             Case "l"c : output &= "poT"
    20.             Case "m"c : output &= "erV"
    21.             Case "n"c : output &= "poV"
    22.             Case "o"c : output &= "zXN"
    23.             Case "p"c : output &= "oIe"
    24.             Case "q"c : output &= "QxG"
    25.             Case "r"c : output &= "lDJ"
    26.             Case "s"c : output &= "RyM"
    27.             Case "t"c : output &= "kSf"
    28.             Case "u"c : output &= "ioN"
    29.             Case "v"c : output &= "xQI"
    30.             Case "w"c : output &= "fZE"
    31.             Case "x"c : output &= "tRe"
    32.             Case "y"c : output &= "aAD"
    33.             Case "z"c : output &= "jjK"
    34.         End Select
    35.     Next character
    36.  
    37.     Return output
    38. End Function
    I've also shortcut the output = output & "xxx" stuff with the &= operator.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width