PHP: Vowels and consonants

Count the vowels and the other letters of a line, and ignore everything that is not a letter.

What to build

A program that counts vowels and consonants.

Requirements

  • Read one line.
  • Print vowels V consonants C. The vowels are a, e, i, o, u in either case; other letters are consonants; digits, spaces and symbols are ignored.

Input

One line of text.

Output

One line.

Constraints

The line has at most 200 characters.

Example

Input:

Hello World

Output:

vowels 3 consonants 7

Why it works

Going through the characters one by one lets you test each for being a letter and a vowel.

Examples

Example 1 · Input
Hello World
Expected output
vowels 3 consonants 7
Example 2 · Input
123 !!
Expected output
vowels 0 consonants 0

Hints

Hint 1

Make the line lower case and look at each character with a for loop and $s[$i].

Hint 2

ctype_alpha tells whether a character is a letter; str_contains("aeiou", $ch) tells whether it is a vowel.

Solution

Show one correct solution (try it yourself first)
<?php
$line = strtolower(trim(fgets(STDIN)));
$vowels = 0;
$consonants = 0;
for ($i = 0; $i < strlen($line); $i++) {
    $ch = $line[$i];
    if (!ctype_alpha($ch)) {
        continue;
    }
    if (str_contains("aeiou", $ch)) {
        $vowels++;
    } else {
        $consonants++;
    }
}
echo "vowels $vowels consonants $consonants\n";

▶ Open this solution in the compiler

Lesson: Strings →