Merge pull request #14 from MakeContributions/feature/palindrome-csharp

feat(strings): add is palindrome example for C#
pull/17/head
Ming Tsai 2021-01-20 11:43:35 -04:00 committed by GitHub
commit 8ff6a99172
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -4,6 +4,11 @@
1. [Palindrome Check](c-or-cpp/palindrome.c) 1. [Palindrome Check](c-or-cpp/palindrome.c)
### C#
You could use any online IDE (for an example [.net Finddle](https://dotnetfiddle.net/)) to test them.
1. [Palindrome Check](csharp/palindrome.cs)
### JavaScript ### JavaScript
1. [Palindrome Check](js/palindrome.js) 1. [Palindrome Check](js/palindrome.js)

View File

@ -0,0 +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);
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;
}
}