PHP: Palindromes

A palindrome reads the same in both directions. Check sentences by looking only at letters and digits.

What to build

A program that tells whether each line is a palindrome.

Requirements

  • For every line, ignore capital letters and every character that is not a letter or a digit.
  • Print yes if what is left reads the same forwards and backwards, otherwise no.

Input

Line 1: n. Then n lines.

Output

n lines.

Constraints

1 ≤ n ≤ 20; each line has at most 100 characters and at least one letter or digit.

Example

Input:

3
A man, a plan, a canal: Panama
hello
Was it a car or a cat I saw?

Output:

yes
no
yes

Why it works

Cleaning first makes the comparison simple: the text is a palindrome when it equals its own reverse.

Examples

Example 1 · Input
3
A man, a plan, a canal: Panama
hello
Was it a car or a cat I saw?
Expected output
yes
no
yes
Example 2 · Input
2
abba
abc
Expected output
yes
no

Hints

Hint 1

Build a clean string with only the letters and digits, in lower case.

Hint 2

Compare the clean string with strrev of itself.

Solution

Show one correct solution (try it yourself first)
<?php
$n = (int) trim(fgets(STDIN));
for ($i = 0; $i < $n; $i++) {
    $line = strtolower(trim(fgets(STDIN)));
    $clean = "";
    for ($j = 0; $j < strlen($line); $j++) {
        if (ctype_alnum($line[$j])) {
            $clean .= $line[$j];
        }
    }
    echo $clean === strrev($clean) ? "yes" : "no", "\n";
}

▶ Open this solution in the compiler

Lesson: Strings →