Skip to content

Instantly share code, notes, and snippets.

@AlexArchive
Created September 8, 2014 15:04
Show Gist options
  • Save AlexArchive/8958555ba6d0e0c683dd to your computer and use it in GitHub Desktop.
Save AlexArchive/8958555ba6d0e0c683dd to your computer and use it in GitHub Desktop.
(F# versus. C#) Primitive form of ROT13
using System;
using System.Linq;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(EncryptText("THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG"));
}
private static string EncryptText(string text)
{
return text.Aggregate("", (s, c) => s + Encrypt(c));
}
private static char Encrypt(char letter)
{
if (char.IsLetter(letter))
{
var a = letter - 'A';
var b = (a + 13) % 26;
var c = b + 'A';
return (char) c;
}
else
{
return letter;
}
}
}
}
open System
let rot13Encrypt (letter : char) =
if Char.IsLetter(letter) then
let newLetter =
(int letter)
|> (fun letterIdx -> letterIdx - (int 'A'))
|> (fun letterIdx -> (letterIdx + 13) % 26)
|> (fun letterIdx -> letterIdx + (int 'A'))
|> char
newLetter
else
letter
let encryptText (text : char[]) =
for idx = 0 to text.Length - 1 do
let letter = text.[idx]
text.[idx] <- rot13Encrypt letter
let text =
Array.ofSeq "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG"
encryptText(text)
printfn "%s" <| new String(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment