$str = readline('Enter string: ');
$str = strtolower($str);
$vowels = 'aeiou';
$vowelsTotal = 0;
$vowelsMap = array();
for ($i = 0; $i < strlen($str); $i++) {
$char = $str[$i];
// check if current char is vowel
if (strpos($vowels, $char)) {
// increase total number
$vowelsTotal++;
// increase number of each vowel
if (array_key_exists($char, $vowelsMap)) {
$vowelsMap[$char]++;
} else {
$vowelsMap[$char] = 1;
}
}
}
echo 'Total vowels: ' . $vowelsTotal . PHP_EOL;
echo 'Occurrences: ' . PHP_EOL;
foreach($vowelsMap as $key => $val) {
echo ' ' . $key . ' - ' . $val . PHP_EOL;
}
Comments
Leave a comment