Drill

ProblemsC# › warmup

Palindrome check

easywarmupStringsTwo pointersC#

A puzzle feature checks whether a phrase reads the same backwards.

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.

Solve it in Python →

Where you start

public bool IsPalindrome(string text) {
    
}

Worked examples

CallResult
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;
}

The same problem in another language

More warmup problems in C#