Q032FreeSystemVerilog
Clean palindrome check
Interview prompt
Question
Return 1 when an ASCII string is a palindrome after ignoring non-alphanumeric bytes and letter case. ASCII letters and digits participate in the comparison.
Candidate starting point
Implementation scaffold
function automatic bit is_alnum(byte c);
// TODO: identify ASCII letters and digits.
endfunction
function automatic byte to_lower(byte c);
// TODO: lowercase ASCII uppercase letters; leave other bytes unchanged.
endfunction
function automatic bit is_palindrome_clean(string s);
// TODO: compare participating bytes from both ends.
endfunctionReviewed example
Trace one case
Input
s = "A man, a plan, a canal: Panama!"Expected output
1 (palindrome)Removing punctuation and folding case produces "amanaplanacanalpanama", which reads the same in both directions.
What to cover
Requirements
- Skip non-alphanumeric bytes from both ends.
- Compare letters case-insensitively.
- Treat an empty cleaned string as a palindrome.
