These are the well known text rotation algorithms. The best known one is likely Rot13. However, I wanted more than just that, so I did rot5 (for numbers), rot18 (for alphanumeric), and rot47 (for most ASCII chars). I hope someone will find these useful.
Function Rot5:String( text:String )
Local out:Int[text.Length]
For Local i:Int = 0 Until text.Length
If text[i] >= 48 And text[i] <= 57
out[i] = 48 + ((text[i] - 43) Mod 10)
Else
out[i] = text[i]
End
Next
Return String.FromChars(out)
End
Function Rot13:String( text:String )
Local out:Int[text.Length]
For Local i:Int = 0 Until text.Length
out[i] = 64 ~ (text[i] & 223)
If out[i] < 27
out[i] = ((text[i] & 96) | (out[i] + 12) Mod 26) + 1
Else
out[i] = text[i]
End
Next
Return String.FromChars(out)
End
Function Rot18:String( text:String )
Local out:Int[text.Length]
For Local i:Int = 0 Until text.Length
out[i] = 64 ~ (text[i] & 223)
If out[i] < 27
out[i] = ((text[i] & 96) | (out[i] + 12) Mod 26) + 1
ElseIf text[i] >= 48 And text[i] <= 57
out[i] = 48 + ((text[i] - 43) Mod 10)
Else
out[i] = text[i]
End
Next
Return String.FromChars(out)
End
Function Rot47:String( text:String )
Local out:Int[text.Length]
For Local i:Int = 0 Until text.Length
If text[i] > 32 And text[i] < 127
out[i] = 33 + ((text[i] + 14) Mod 94)
Else
out[i] = text[i]
End
Next
Return String.FromChars(out)
End Example usageFunction Main:Int()
Print "=== Rot 5 ==="
Print "Original: 0213465789"
Print "Rotated : " + Rot5("0213465789")
Print "RotAgain: " + Rot5(Rot5("0213465789"))
Print "=== Rot 13 ==="
Print "Original: Hello World"
Print "Rotated : " + Rot13("Hello World")
Print "RotAgain: " + Rot13(Rot13("Hello World"))
Print "=== Rot 18 ==="
Print "Original: Hello World 0213465789"
Print "Rotated : " + Rot18("Hello World 0213465789")
Print "RotAgain: " + Rot18(Rot18("Hello World 0213465789"))
Print "=== Rot 47 ==="
Print "Original: Hello World"
Print "Rotated : " + Rot47("Hello World")
Print "RotAgain: " + Rot47(Rot47("Hello World"))
End
|