Vowel Count

kata programming

مساله:

تعداد کاراکترهای صدا دار را در رشته داده شده، برگردانید.

ما صدا های کاراکتر را به صورت زیر در نظر می گیریم

aeiou

داده های ورودی همه با حروف کوچک هستند


Description:

Return the number (count) of vowels in the given string.

We will consider aeiou as vowels for this Kata (but not y).

The input string will only consist of lower case letters and/or spaces.


راه حل ها:

function getCount($str) {
  $vowelsCount = 0;
  
  $str = strtolower($str);
  foreach (count_chars($str, 1) as $i => $val) {
      if (in_array(chr($i), ['a','e','i','o','u']))
          $vowelsCount += $val;
  }
  
  return $vowelsCount;
}
function getCount($str) {;
  
  return preg_match_all('/[aeiou]/i',$str,$matches);
}
function getCount($str) {
  $vowelsCount = 0;
  $vowels = ['a', 'e', 'i', 'o', 'u'];
  // enter your magic here
  foreach($vowels as $vowel) {
    $vowelsCount += substr_count($str, $vowel);
  }
  
  
  return $vowelsCount;
}

دیدگاهتان را بنویسید