DEV Community

Discussion on: Daily Challenge #47 - Alphabets

Collapse
 
peter279k profile image
peter279k

Here is the simple solution with PHP:

function alphabet_position(string $s): string {
    $s = strtoupper($s);
    $alphabetNums = range(65, 90);
    $alphabets = [];

    foreach ($alphabetNums as $chr) {
        $aphabets[] = chr($chr);
    }

    $result = "";

    $index = 0;
    for (; $index < strlen($s); $index++) {
        if (in_array($s[$index], $aphabets) === false) {
            continue;
        }

        $result .= (string)(ord($s[$index]) - 64) . " ";
    }

    return substr($result, 0, -1);
}