(#46) Add string.IsPalindrome

This commit is contained in:
Oliver Booth 2022-02-14 15:57:29 +00:00
parent 7fd1b88acf
commit 334e706b68
1 changed files with 49 additions and 0 deletions

View File

@ -203,6 +203,55 @@ namespace X10D
return true;
}
/// <summary>
/// Determines whether the current string is considered palindromic; that is, the letters within the string are the
/// same when reversed.
/// </summary>
/// <param name="value">The value to check.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="value" /> is considered a palindromic string; otherwise,
/// <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
public static bool IsPalindrome(this string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
if (value.Length == 0)
{
// empty string is not palindromic
return false;
}
for (int index = 0, endIndex = value.Length - 1; index < value.Length; index++, endIndex--)
{
Rune startRune = new Rune(value[index]);
Rune endRune = new Rune(value[endIndex]);
if (!Rune.IsLetter(startRune) && !Rune.IsNumber(startRune))
{
endIndex++;
continue;
}
if (!Rune.IsLetter(endRune) && !Rune.IsNumber(endRune))
{
index--;
continue;
}
if (Rune.ToUpperInvariant(startRune) != Rune.ToUpperInvariant(endRune))
{
return false;
}
}
return true;
}
/// <summary>
/// Determines if all alpha characters in this string are considered uppercase.
/// </summary>