1 Star 0 Fork 0

jobily/TheAlgorithms-C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Palindrome.cs 1.22 KB
一键复制 编辑 原始数据 按行查看 历史
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);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hubo/the-algorithms-c-sharp.git
git@gitee.com:hubo/the-algorithms-c-sharp.git
hubo
the-algorithms-c-sharp
TheAlgorithms-C-Sharp
master

搜索帮助