Advertisement
Daimond1980

My "IsPalindrome" solution

Oct 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         static bool IsPalindrome(string s)
  2.         {
  3.             if (s == null)
  4.                 throw new NullReferenceException(); // if we don't get a string throw exception
  5.  
  6.             // Remove all commas, spaces and so on (except letters) and switch to lowcase.
  7.             // For cases like "Never, Odd Or Even."
  8.             s = Regex.Replace(s, "[^a-zA-Z]", "").ToLower();
  9.  
  10.             if (s == "")
  11.                 return true; // or "false". Really I don't know if empty string Palindrome or not. So it is depended on our agreements
  12.  
  13.             var arr = s.ToCharArray();
  14.             return arr.SequenceEqual(arr.Reverse());
  15.         }
  16.  
  17.         static void Check(string s, bool shouldBePalindrome)
  18.         {
  19.             Console.WriteLine(IsPalindrome(s) == shouldBePalindrome ? "pass" : "FAIL");
  20.         }
  21.  
  22.         static void Main()
  23.         {
  24.             //Check(null, false);
  25.             Check("", false);
  26.             Check("abcba", true);
  27.             Check("abcde", false);
  28.             Check("Mr owl ate my metal worm", true);
  29.             Check("Never, Odd Or Even.", true);
  30.             Check("Never Even Or Odd", false);
  31.  
  32.             Console.ReadLine();
  33.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement