Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static bool IsPalindrome(string s)
- {
- if (s == null)
- throw new NullReferenceException(); // if we don't get a string throw exception
- // Remove all commas, spaces and so on (except letters) and switch to lowcase.
- // For cases like "Never, Odd Or Even."
- s = Regex.Replace(s, "[^a-zA-Z]", "").ToLower();
- if (s == "")
- return true; // or "false". Really I don't know if empty string Palindrome or not. So it is depended on our agreements
- var arr = s.ToCharArray();
- return arr.SequenceEqual(arr.Reverse());
- }
- static void Check(string s, bool shouldBePalindrome)
- {
- Console.WriteLine(IsPalindrome(s) == shouldBePalindrome ? "pass" : "FAIL");
- }
- static void Main()
- {
- //Check(null, false);
- Check("", false);
- Check("abcba", true);
- Check("abcde", false);
- Check("Mr owl ate my metal worm", true);
- Check("Never, Odd Or Even.", true);
- Check("Never Even Or Odd", false);
- Console.ReadLine();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement