Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- public static void Main()
- {
- var userInput = GetUserInput();
- var output = IsPalindrome(userInput) ? "YES" : "NO";
- Console.WriteLine(output);
- }
- /// <Summary>
- /// We take the user's input and clean out all non alphabetic characters. According to the spec we only want upper and lower
- /// case letters from the english alphabet.
- /// </Summary>
- private static string GetUserInput()
- {
- var rawInput = Console.ReadLine().Trim();
- /*
- This pattern will capture all upper and lower case letters. If the letters are separated by bad characters it will
- put them in separate groups that need to be accumulated according to this function's spec.
- i.e. Regex.Matches(pattern, "jepg.ergj") =>
- $1 = "jepg";
- $2 = "ergj";
- so our final result will be $1 + $2 = "jepgergj"
- */
- var matches = Regex.Matches(rawInput, @"([A-Za-z])+");
- var result = new StringBuilder();
- foreach (var match in matches)
- {
- result.Append(match.ToString());
- }
- return result.ToString();
- }
- /// <Summary>
- /// Will detect if the string is the same reading left-to-right and right-to-left.
- /// </Summary>
- private static bool IsPalindrome(string input)
- {
- // the one-liner for reversing a string using LINQ. It is less efficient than other methods, if IsPalindrome is called
- // often, I recommend this guy for refactoring into a more efficient low level loop
- var reverse = new string(input.ToCharArray().Reverse().ToArray());
- return reverse == input;
- }
- }
Add Comment
Please, Sign In to add comment