Guest User

Untitled

a guest
Mar 23rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. class Program
  2. {
  3.  
  4. public static void Main()
  5. {
  6. var userInput = GetUserInput();
  7.  
  8. var output = IsPalindrome(userInput) ? "YES" : "NO";
  9.  
  10. Console.WriteLine(output);
  11. }
  12.  
  13. /// <Summary>
  14. /// We take the user's input and clean out all non alphabetic characters. According to the spec we only want upper and lower
  15. /// case letters from the english alphabet.
  16. /// </Summary>
  17. private static string GetUserInput()
  18. {
  19. var rawInput = Console.ReadLine().Trim();
  20. /*
  21. This pattern will capture all upper and lower case letters. If the letters are separated by bad characters it will
  22. put them in separate groups that need to be accumulated according to this function's spec.
  23.  
  24. i.e. Regex.Matches(pattern, "jepg.ergj") =>
  25. $1 = "jepg";
  26. $2 = "ergj";
  27. so our final result will be $1 + $2 = "jepgergj"
  28. */
  29. var matches = Regex.Matches(rawInput, @"([A-Za-z])+");
  30. var result = new StringBuilder();
  31. foreach (var match in matches)
  32. {
  33. result.Append(match.ToString());
  34. }
  35. return result.ToString();
  36. }
  37.  
  38. /// <Summary>
  39. /// Will detect if the string is the same reading left-to-right and right-to-left.
  40. /// </Summary>
  41. private static bool IsPalindrome(string input)
  42. {
  43. // the one-liner for reversing a string using LINQ. It is less efficient than other methods, if IsPalindrome is called
  44. // often, I recommend this guy for refactoring into a more efficient low level loop
  45. var reverse = new string(input.ToCharArray().Reverse().ToArray());
  46. return reverse == input;
  47. }
  48. }
Add Comment
Please, Sign In to add comment