Simple rot13 encoder in vb.net

前端 未结 3 1693
隐瞒了意图╮
隐瞒了意图╮ 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:24

    Looks like people are giving good answers to this but here's my try at it.

    Dim input As String = "This is a Test!! Guvf vf n Grfg!!"
    Dim result As StringBuilder = New StringBuilder()
    
    For Each ch As Char In input
    
        If (Not Char.IsLetter(ch)) Then
            result.Append(ch)
            Continue For
        End If
    
        Dim checkIndex As Integer = Asc("a") - (Char.IsUpper(ch) * -32)
        Dim index As Integer = ((Asc(ch) - checkIndex) + 13) Mod 26
    
        result.Append(Chr(index + checkIndex))
    
    Next
    
    Console.WriteLine(result.ToString())
    

    EDIT: improved to remove need for uppercase check. This will properly handle case and special characters with only 1 if statement inside the loop.

提交回复
热议问题