Skip to content

Instantly share code, notes, and snippets.

@Soben
Last active August 14, 2023 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Soben/fca6aab156a8237d1fc6ef353ab3860e to your computer and use it in GitHub Desktop.
Save Soben/fca6aab156a8237d1fc6ef353ab3860e to your computer and use it in GitHub Desktop.
Cassidoo | Faulty Keyboard

Solution to Cassidoo's Week of Aug 14, 2023 Challenge

You have a faulty keyboard. Whenever you type a vowel on it (a,e,i,o,u,y), it reverses the string that you have written, instead of typing the character. Typing other characters works as expected. Given a string, return what will be on the screen after typing with your faulty keyboard.

<?php
function faultyKeeb($string, $result = '') {
if ($string == '') return $result;
$char = $string[0];
if (in_array(strtolower($char), ['a', 'e', 'i', 'o', 'u', 'y'])) {
return faultyKeeb(substr($string, 1), strrev($result));
}
return faultyKeeb(substr($string, 1), $result . $char);
}
var_dump(faultyKeeb('string')); // rtsng
var_dump(faultyKeeb('hello world!')); // w hllrld!
var_dump(faultyKeeb("Onomatopoeia")); // tnmp
var_dump(faultyKeeb("Chris Lagasse")); // ssL sChrg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment