Simple rot13 encoder in vb.net

前端 未结 3 1694
隐瞒了意图╮
隐瞒了意图╮ 2021-01-29 04:38

I am looking for a simple way to encode an inputted text into Rot13. I am hitting a brick wall at the stage of being able to separate out words into individual characters and in

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-29 05:27

    It seems like you're making this way harder than it has to be. No need to separate words, etc, and definitely no need for a large If/Else block:

    Public Function Rot13(ByVal input As String) As String
        Dim result As Char() = input.ToCharArray()
    
        For i As Integer = 0 To result.Length - 1
            Dim temp As Integer = Asc(result(i))
            Select Case temp
               Case 65 to 77, 97 To 109 'A - M
                   result(i) = Chr(temp + 13)
               Case 78 to 90, 110 To 122 'N - Z
                   result(i) = Chr(temp - 13)
            End Select
        Next i
    
        Return New String(result)
    End Function
    

    Note that this was entered directly into the browser window and is completely untested.

提交回复
热议问题