DEV Community

Discussion on: Daily Challenge #34 - WeIrD StRiNg CaSe

Collapse
 
peter279k profile image
peter279k

Here is the simple solution with PHP:

function toWeirdCase($string) {
    $strings = explode(' ', $string);

    $stringsIndex = 0;
    foreach ($strings as $string) {
      $index = 0;
      for (; $index < strlen($string); $index++) {
        if ($index % 2 === 0) {
          $string[$index] = strtoupper($string[$index]);
        } else {
          $string[$index] = strtolower($string[$index]);
        }
      }

      $strings[$stringsIndex] = $string;
      $stringsIndex++;
    }

    return implode(' ', $strings);
}