forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Palindrome.cs
38 lines (34 loc) · 1.22 KB
/
Palindrome.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Text.RegularExpressions;
namespace Algorithms.Strings;
/// <summary>
/// Palindrome a series of characters or a string that when reversed,
/// equals the original string.
/// </summary>
public static class Palindrome
{
/// <summary>
/// Function to check if a string is a palindrome.
/// </summary>
/// <param name="word">String being checked.</param>
public static bool IsStringPalindrome(string word) =>
TypifyString(word).Equals(TypifyString(ReverseString(word)));
/// <summary>
/// Typify string to lower and remove white spaces.
/// </summary>
/// <param name="word">String to remove spaces.</param>
/// <returns>Returns original string without spaces.</returns>
private static string TypifyString(string word) =>
Regex.Replace(word.ToLowerInvariant(), @"\s+", string.Empty);
/// <summary>
/// Helper function that returns a reversed string inputed.
/// </summary>
/// <param name="s">String to be reversed.</param>
/// <returns>Returns s reversed.</returns>
private static string ReverseString(string s)
{
var arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}