diff --git a/C#/main.cs b/C#/main.cs index 05e3ce2..ed72952 100644 --- a/C#/main.cs +++ b/C#/main.cs @@ -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());