From dbfcc3dfc208d1f279cfd99557e611a678e959c8 Mon Sep 17 00:00:00 2001 From: Haydn Kane Date: Thu, 4 Mar 2021 00:35:47 +0000 Subject: [PATCH] C# port of Name --- 63 Name/csharp/Name.csproj | 8 ++++++ 63 Name/csharp/Name.sln | 25 +++++++++++++++++ 63 Name/csharp/Program.cs | 44 ++++++++++++++++++++++++++++++ 63 Name/csharp/StringExtensions.cs | 41 ++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 63 Name/csharp/Name.csproj create mode 100644 63 Name/csharp/Name.sln create mode 100644 63 Name/csharp/Program.cs create mode 100644 63 Name/csharp/StringExtensions.cs diff --git a/63 Name/csharp/Name.csproj b/63 Name/csharp/Name.csproj new file mode 100644 index 00000000..c73e0d16 --- /dev/null +++ b/63 Name/csharp/Name.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/63 Name/csharp/Name.sln b/63 Name/csharp/Name.sln new file mode 100644 index 00000000..931d9288 --- /dev/null +++ b/63 Name/csharp/Name.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31005.135 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Name", "Name.csproj", "{B63B5D5C-C5E0-4EFE-9EF4-292915DCC22D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B63B5D5C-C5E0-4EFE-9EF4-292915DCC22D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B63B5D5C-C5E0-4EFE-9EF4-292915DCC22D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B63B5D5C-C5E0-4EFE-9EF4-292915DCC22D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B63B5D5C-C5E0-4EFE-9EF4-292915DCC22D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FCD5BEAE-D35E-49A7-B8F3-B8B3F4A8C624} + EndGlobalSection +EndGlobal diff --git a/63 Name/csharp/Program.cs b/63 Name/csharp/Program.cs new file mode 100644 index 00000000..a9c817f3 --- /dev/null +++ b/63 Name/csharp/Program.cs @@ -0,0 +1,44 @@ +using System; + +namespace Name +{ + public class Program + { + static void Main(string[] args) + { + Console.WriteLine("NAME".CentreAlign()); + Console.WriteLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".CentreAlign()); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine("HELLO."); + Console.WriteLine("MY NAME IS CREATIVE COMPUTER."); + Console.Write("WHAT'S YOUR NAME (FIRST AND LAST? "); + var name = Console.ReadLine(); + Console.WriteLine(); + Console.WriteLine($"THANK YOU, {name.Reverse()}."); + Console.WriteLine("OOPS! I GUESS I GOT IT BACKWARDS. A SMART"); + Console.WriteLine("COMPUTER LIKE ME SHOULDN'T MAKE A MISTAKE LIKE THAT!"); + Console.WriteLine(); + Console.WriteLine("BUT I JUST NOTICED YOUR LETTERS ARE OUT OF ORDER."); + Console.WriteLine($"LET'S PUT THEM IN ORDER LIKE THIS: {name.Sort()}"); + Console.WriteLine(); + Console.Write("DON'T YOU LIKE THAT BETTER? "); + var like = Console.ReadLine(); + Console.WriteLine(); + + if (like.ToUpperInvariant() == "YES") + { + Console.WriteLine("I KNEW YOU'D AGREE!!"); + } + else + { + Console.WriteLine("I'M SORRY YOU DON'T LIKE IT THAT WAY."); + } + + Console.WriteLine(); + Console.WriteLine($"I REALLY ENJOYED MEETING YOU {name}."); + Console.WriteLine("HAVE A NICE DAY!"); + } + } +} diff --git a/63 Name/csharp/StringExtensions.cs b/63 Name/csharp/StringExtensions.cs new file mode 100644 index 00000000..8003fa38 --- /dev/null +++ b/63 Name/csharp/StringExtensions.cs @@ -0,0 +1,41 @@ +using System; + +namespace Name +{ + public static class StringExtensions + { + private const int ConsoleWidth = 120; // default console width + + public static string CentreAlign(this string value) + { + int spaces = ConsoleWidth - value.Length; + int leftPadding = spaces / 2 + value.Length; + + return value.PadLeft(leftPadding).PadRight(ConsoleWidth); + } + + public static string Reverse(this string value) + { + if (value is null) + { + return null; + } + + char[] characterArray = value.ToCharArray(); + Array.Reverse(characterArray); + return new String(characterArray); + } + + public static string Sort(this string value) + { + if (value is null) + { + return null; + } + + char[] characters = value.ToCharArray(); + Array.Sort(characters); + return new string(characters); + } + } +}