Palindrome check
A puzzle feature checks whether a phrase reads the same backwards.
- Compare letters and digits only — skip spaces and punctuation.
- Case does not matter.
- An empty string counts as a palindrome.
IsPalindrome(text: string) → bool
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public bool IsPalindrome(string text) {
}
Worked examples
| Call | Result |
|---|---|
IsPalindrome("") | true |
IsPalindrome("racecar") | true |
IsPalindrome("A man, a plan, a canal: Panama") | true |
IsPalindrome("hello") | false |
Hint
Walk one index from each end, skipping anything that is not alphanumeric.
Reference solution in C#
public bool IsPalindrome(string text) {
var s = new string(text.ToLowerInvariant().Where(char.IsLetterOrDigit).ToArray());
for (int i = 0, j = s.Length - 1; i < j; i++, j--) {
if (s[i] != s[j]) return false;
}
return true;
}