Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions C#/main.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
using System;
using System.Linq;

public class Rot13
{
public static void Main()
{
Console.Write("Enter string to encode: ");
Console.Write(String.Format("Encoded string: {0}\n",
new String(
Console.ReadLine().ToCharArray().Select(c => {
if('a' <= c && c <= 'z') c = (char)((c - 'a' + 13) % 26 + 'a');
else if('A' <= c && c <= 'Z') c = (char)((c - 'A' + 13) % 26 + 'A');
return c;
}).ToArray()
)
));
}
}
// Using top-level statements (see https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/top-level-statements)
// and Linq along with pattern matching (see https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching)
if (args?.Count() > 0)
Console.WriteLine(args[0].Select(c =>
c switch
{
>= 'a' and <= 'm' or >= 'A' and <= 'M' => (char)(c + 13),
>= 'n' and <= 'z' or >= 'N' and <= 'Z' => (char)(c - 13),
_ => c
}
).ToArray());