Write a function that checks if a given sentence is a palindrome. A palindrome is a word, phrase, verse, or sentence that reads the same backward or forward. Only the order of English alphabet letters (A-Z and a-z) should be considered, other characters should be ignored.
For example, IsPalindrome("Noel sees Leon.") should return true as spaces, period, and case should be ignored resulting with "noelseesleon" which is a palindrome since it reads same backward and forward.
Code Snippet
- using System;
- using System.Linq;
- using System.Text.RegularExpressions;
- public class Palindrome
- {
- public static bool IsPalindrome(string str)
- {
- string tmpStr = str.ToLower().Trim();
- tmpStr = Regex.Replace(tmpStr, @"[^a-zA-Z]+", ""); //Replace all the special characters
- return tmpStr.SequenceEqual(tmpStr.Reverse());
- }
- public static void Main(string[] args)
- {
- Console.WriteLine(IsPalindrome("Noel sees Leon."));
- }
- }
No comments:
Post a Comment