DEV Community

loading...

Discussion on: Daily Challenge #39 - Virus

Collapse
itsdarrylnorris profile image
Darryl Norris

PHP 💻

<?php

/**
 * Antivirus
 * @param  string $string
 * @return string
 */
function antivirus(string $string): string
{
  return implode(".", array_map(function($sentence) {
    $sentence = str_split($sentence);
    foreach ($sentence as $key => $value) {
      if ($key === 0 && $value === ' ') continue;
      else $sentence[$key] = strtoupper($value); break;
    }
    return implode("",$sentence);
  }, explode(".", str_replace('ie', 'ei', strtolower($string)))));
}


echo antivirus('He haD iEght ShOTs of CAffIEne. aFter thaT HE WenT tO SleeP.');
// Output: He had eight shots of caffeine. After that he went to sleep.