From 6115a71348a7a302a68bc5681f7b8faeb6c692d3 Mon Sep 17 00:00:00 2001 From: Ming Tsai Date: Tue, 19 Jan 2021 09:38:24 -0400 Subject: [PATCH 1/3] feat(strings): add is palindrome example for C# --- strings/README.md | 4 ++++ strings/csharp/palindrome.cs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 strings/csharp/palindrome.cs diff --git a/strings/README.md b/strings/README.md index 362cc48a..59917c49 100644 --- a/strings/README.md +++ b/strings/README.md @@ -3,3 +3,7 @@ ### C or C++ 1. [Palindrome Check](c-or-cpp/palindrome.c) + +### C# +Please use [.Net Finddle](https://dotnetfiddle.net/) +1. [Palindrome Check](csharp/palindrome.cs) diff --git a/strings/csharp/palindrome.cs b/strings/csharp/palindrome.cs new file mode 100644 index 00000000..7a4ced1c --- /dev/null +++ b/strings/csharp/palindrome.cs @@ -0,0 +1,19 @@ +using System; +using System.Linq; + +public class Program +{ + public static void Main() + { + bool result = false; + result = IsPalindrome("abba"); + Console.WriteLine(result); + result = IsPalindrome("abbccbba"); + Console.WriteLine(result); + } + + private static bool IsPalindrome(string source) { + string reverse = new string(Enumerable.Range(1, source.Length).Select(i => source[source.Length - i]).ToArray()); + return reverse == source; + } +} \ No newline at end of file From b7e7e7d272467e8dbb7e92b79171836f9c64d03b Mon Sep 17 00:00:00 2001 From: Ming Date: Wed, 20 Jan 2021 09:17:51 -0400 Subject: [PATCH 2/3] docs: make more clearly about on use IDE --- strings/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/strings/README.md b/strings/README.md index 8b37427b..8e3e7af8 100644 --- a/strings/README.md +++ b/strings/README.md @@ -5,7 +5,8 @@ 1. [Palindrome Check](c-or-cpp/palindrome.c) ### C# -Please use [.Net Finddle](https://dotnetfiddle.net/) +You could use any online IDE (for an example [.net Finddle](https://dotnetfiddle.net/)) to test them. + 1. [Palindrome Check](csharp/palindrome.cs) ### JavaScript From 1a2159d664a2059848939f37ebf6bfbb7cef5a5a Mon Sep 17 00:00:00 2001 From: Ming Date: Wed, 20 Jan 2021 09:25:08 -0400 Subject: [PATCH 3/3] chore(strings): replace special character --- strings/csharp/palindrome.cs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/strings/csharp/palindrome.cs b/strings/csharp/palindrome.cs index 7a4ced1c..b01d70cb 100644 --- a/strings/csharp/palindrome.cs +++ b/strings/csharp/palindrome.cs @@ -1,19 +1,24 @@ using System; using System.Linq; - +using System.Text.RegularExpressions; + public class Program { - public static void Main() - { - bool result = false; - result = IsPalindrome("abba"); - Console.WriteLine(result); - result = IsPalindrome("abbccbba"); - Console.WriteLine(result); - } - - private static bool IsPalindrome(string source) { - string reverse = new string(Enumerable.Range(1, source.Length).Select(i => source[source.Length - i]).ToArray()); - return reverse == source; - } + public static void Main() + { + bool result = false; + result = IsPalindrome("abba"); + Console.WriteLine(result); + result = IsPalindrome("abbccbbA"); + Console.WriteLine(result); + result = IsPalindrome("Mr. Owl ate my metal worm"); + Console.WriteLine(result); + } + + private static bool IsPalindrome(string source) { + source = source.ToLower(); + source = Regex.Replace(source, @"[^0-9A-Za-z]", ""); + string reverse = new string(Enumerable.Range(1, source.Length).Select(i => source[source.Length - i]).ToArray()); + return reverse == source; + } } \ No newline at end of file