How can I rotate only the letters using rot13 in Visual Basic?

1k views Asked by At

I'm working on the final touches of my programming finals encoder program and i have build a rotation function that leave all characters that are not letters the way they are(so basically everything else, numbers, dashes, spaces, remain the same and all letters upper and lower rotate normally)

            n = 13
        For i = 1 To Len(stinput)
            If  (i >= 65 And i <= 90) Or (i >= 97 And i <= 122) Then
                Mid(stinput, i, 1) = Chr(Asc(Mid(stinput, i, 1)) - n)
            End If
        Next i

with the if function I'm trying to separate the ascii codes that are not letters, only problem is i have a bit of trouble getting it to work, the code above is not functional.

any and all help is appreciated thanks!

2

There are 2 answers

0
Viralwarrior012 On BEST ANSWER

' this is the encryption part of the program I figured it out, heres how to change only the selected characters upon rotation

 Dim stinput As String = txtinput.Text
    Dim i, n, m, o As Integer
    Dim inleftover As Integer
    If RBRot13.Checked = False Then                 'if neither rotation button is selected, a message will tell user to select one
        If RBActiveSelect.Checked = False Then
            MsgBox("Vous devez choisir le nombre de rotation afin de poursuivre a l'encryption")
        End If
    End If
    txtoutput.Text = ""                             'Clears output box when button is pressed to only show the result
    If RBRot13.Checked = True Then
        For i = 1 To stinput.Length
            n = Asc(Mid(stinput, i, 1))             'Defines the position of the caracter in the ascii table
            Select Case n
                Case 65 To 90 : o = n + 13          'From A(65) to Z(90), adds 13 to their respective ascii number
                    If o > 90 And o <= 103 Then     'if the addition is between 90 and 103(90 +13) 
                        inleftover = o - 91         'Calculate de difference as a leftover 
                        o = 65 + inleftover         'Apply leftover to A(65) so as to not give anything other than a letter
                    End If
                Case 97 To 122 : o = n + 13         'From a(97) to z(122), adds 13 to their respective ascii number
                    If o > 122 And o <= 135 Then    'if the addition is between 122 and 135(122 +13)
                        inleftover = o - 123        'Calculate de difference as a leftover 
                        o = 97 + inleftover         'Apply leftover to a(97) so as to not give anything other than a letter
                    End If
                Case 192 To 219 : o = n + 13        'From À(192) to Z(219), adds 13 to their respective ascii number
                    If o > 219 And o <= 232 Then    'if the addition is between 219 and 232(219 +13)
                        inleftover = o - 220        'Calculate de difference as a leftover 
                        o = 192 + inleftover         'Apply leftover to À(192) so as to not give anything other than an accented letter
                    End If
                Case 224 To 251 : o = n + 13        'From à(224) to Z(251), adds 13 to their respective ascii number
                    If o > 251 And o <= 264 Then    'if the addition is between 251 and 264(251 +13)
                        inleftover = o - 225        'Calculate de difference as a leftover 
                        o = 224 + inleftover         'Apply leftover to à(224) so as to not give anything other than an accented letter
                    End If
                Case Else : o = n
            End Select
    txtoutput.Text = txtoutput.Text & Chr(o)
        Next i

    ElseIf RBActiveSelect.Checked = True Then
        m = Me.NUDChoice.Value                      'Defines the value of the rotation set by the user
        For i = 1 To stinput.Length
            n = Asc(Mid(stinput, i, 1))             'Defines the position of the caracter in the ascii table
            Select Case n
                Case 65 To 90 : o = n + m           'From A(65) to Z(90), adds m to their respective ascii number
                    If o > 90 Then                  'if the addition is over 90 
                        inleftover = o - 91         'Calculate de difference as a leftover 
                        o = 65 + inleftover         'Apply leftover to A(65) so as to not give anything other than a letter
                    End If
                Case 97 To 122 : o = n + m          'From A(65) to Z(90), adds m to their respective ascii number
                    If o > 122 Then                 'if the addition is over 122
                        inleftover = o - 123        'Calculate de difference as a leftover 
                        o = 97 + inleftover         'Apply leftover to A(65) so as to not give anything other than a letter
                    End If
                Case 192 To 219 : o = n + m         'From À(192) to Z(219), adds m to their respective ascii number
                    If o > 219 Then                 'if the addition is over 219
                        inleftover = o - 220        'Calculate de difference as a leftover 
                        o = 192 + inleftover         'Apply leftover to A(192) so as to not give anything other than an accented letter
                    End If
                Case 224 To 251 : o = n + m         'From à(224) to Z(251), adds m to their respective ascii number
                    If o > 251 Then                 'if the addition is over 251
                        inleftover = o - 225        'Calculate de difference as a leftover 
                        o = 224 + inleftover         'Apply leftover to à(224) so as to not give anything other than an accented letter
                    End If
                Case Else : o = n
            End Select
            txtoutput.Text = txtoutput.Text & Chr(o)
        Next i
    End If
End Sub

'here starts the decryption part of the 2 buttons used

 Dim stinput As String = txtinput.Text
    Dim i, n, m, o As Integer
    Dim inleftover As Integer
    If RBRot13.Checked = False Then                 'if neither rotation button is selected, a message will tell user to select one
        If RBActiveSelect.Checked = False Then
            MsgBox("Vous devez choisir le nombre de rotation afin de poursuivre a la décryption")
        End If
    End If
    txtoutput.Text = ""                             'Clears output box when button is pressed to only show the result
    If RBRot13.Checked = True Then                  'If Rotation 13 selected do
        For i = 1 To stinput.Length
            n = Asc(Mid(stinput, i, 1))             'Defines the position of the caracter in the ascii table
            Select Case n
                Case 65 To 90 : o = n - 13          'From A(65) to Z(90), deducts 13 to their respective ascii number
                    If o < 65 And o >= 52 Then     'if the addition is between 65 and 52(65 - 13) 
                        inleftover = o - 64         'Calculate de difference as a leftover 
                        o = 90 + inleftover         'Apply leftover to A(65) so as to not give anything other than a letter
                    End If
                Case 97 To 122 : o = n - 13         'From a(97) to z(122), deducts 13 to their respective ascii number
                    If o < 97 And o >= 84 Then    'if the addition is between 97 and 84(97 - 13)
                        inleftover = o - 96         'Calculate de difference as a leftover 
                        o = 122 + inleftover         'Apply leftover to a(97) so as to not give anything other than a letter
                    End If
                Case 192 To 219 : o = n - 13        'From À(192) to Z(219), deducts 13 to their respective ascii number
                    If o < 192 And o >= 179 Then    'if the addition is between 192 and 179(192 - 13)
                        inleftover = o - 191        'Calculate de difference as a leftover 
                        o = 219 + inleftover        'Apply leftover to À(192) so as to not give anything other than an accented letter
                    End If
                Case 224 To 251 : o = n - 13        'From à(224) to Z(251), deducts 13 to their respective ascii number
                    If o < 224 And o >= 211 Then    'if the addition is between 224 and 211(224 - 13)
                        inleftover = o - 223        'Calculate de difference as a leftover 
                        o = 251 + inleftover        'Apply leftover to à(224) so as to not give anything other than an accented letter
                    End If
                Case Else : o = n                   'everything else stays the same
            End Select
            txtoutput.Text = txtoutput.Text & Chr(o)    'Prints the result into the output box
        Next i

    ElseIf RBActiveSelect.Checked = True Then       'If Choice rotation selected do
        m = Me.NUDChoice.Value                      'Defines the value of the rotation set by the user
        For i = 1 To stinput.Length
            n = Asc(Mid(stinput, i, 1))             'Defines the position of the caracter in the ascii table
            Select Case n
                Case 65 To 90 : o = n - m           'From A(65) to Z(90), deducts m to their respective ascii number
                    If o < 65 Then                  'if the addition is under 65 
                        inleftover = o - 64         'Calculate de difference as a leftover 
                        o = 65 + inleftover         'Apply leftover to z(90) so as to not give anything other than a letter
                    End If
                Case 97 To 122 : o = n - m          'From a(97) to z(122), deducts m to their respective ascii number
                    If o < 97 Then                  'if the addition is under 97
                        inleftover = o - 96         'Calculate de difference as a leftover 
                        o = 90 + inleftover         'Apply leftover to z(122) so as to not give anything other than a letter
                    End If
                Case 192 To 219 : o = n - m         'From À(192) to Û(219), deducts m to their respective ascii number
                    If o < 192 Then                 'if the addition is under 192
                        inleftover = o - 191        'Calculate de difference as a leftover 
                        o = 219 + inleftover        'Apply leftover to Û(219) so as to not give anything other than an accented letter
                    End If
                Case 224 To 251 : o = n - m         'From à(224) to û(251), deducts m to their respective ascii number
                    If o < 224 Then                 'if the addition is under 224
                        inleftover = o - 223        'Calculate de difference as a leftover 
                        o = 251 + inleftover        'Apply leftover to û(251) so as to not give anything other than an accented letter
                    End If
                Case Else : o = n                   'everything else stays the same
            End Select
            txtoutput.Text = txtoutput.Text & Chr(o)    'Prints the result to the output box
        Next i
    End If
End Sub

Its long yet function, and by the way the m function is linked to a listing button with choice of rotation between 1 and 25

Thanks

0
Enigmativity On

Try this:

Dim rot13c As Func(Of Char, Char, Char) = Function(b, c) Chr(If(c >= b And Asc(c) <= Asc(b) + 25, If(Asc(c) + 13 > Asc(b) + 25, Asc(c) - 13, Asc(c) + 13), Asc(c)))
Dim rot13 As Func(Of String, String) = Function(s) New String(s.Select(Function (c) rot13c("a"C, rot13c("A"C, c))).ToArray())

Then you can call it like this:

Dim text = "Hello, there. This is a test!"
Dim text13 = rot13(text) ' = "Uryyb, gurer. Guvf vf n grfg!"
Dim text1313 = rot13(text13) ' = "Hello, there. This is a test!"