PHP Program to count vowel and consonants in a sentence

What are vowels and consonants in a string?

As we all know the vowels are 'a', 'e', 'i', 'o', and 'u', and all the rest of the characters in alphabets are consonants. In this program, we are counting the number of vowels and consonants in the inputted string or sentence. For example, if the entered string is learnetutorials.com then the number of vowels will be and the number of consonants will be 10. We are also converting the input into the lower case using the built-in function strtolower()

Syntax for is 


strtolower(variable or string);
 

How to count vowels and consonants using a user-defined function in PHP?

In this program, we are counting the constants and vowels in the string or sentence. For this first, we have to read the string or sentence from the user and assign it to the variable text then we have to call the user-defined function countVowelAndConsonants() with the value of the variable text as the argument. And in the function, we have to convert the value of the variable text into the lower case using the built-in function strtolowe() and assign it to the variable str. Now assign the value 0 into variable countV and countC to store the count of vowels and consonants. Then we have to find the length of the variable text. And we have to perform the for loop to check every character of the value of variable str in for loop we first have to assign the value 0 into the available and perform the loop until the condition 'i < len' becomes false and also increment the value of variable  in every iteration and in the block of the loop we have to check the condition 'str[i]' is equal to any of 'a, e, i, o, u' it true add 1 with the value of variable countV and assign it to the variable otherwise check the condition 'str[i] >= "a" && str[i] <= "z"' if true add 1 with the value of variable countC and assign it to the variable countC otherwise perform the next iteration. and after the completion of the loop print the values of the variables countV and countC as the count of vowels and consonants.

ALGORITHM

Step 1: Accept the string from the user and assign it to the variable text

Step 2: Call the function countVowelAndConsonants() with the value of the variable text as the argument

ALGORITHM function: countVowelAndConsonants(text)

Step 1: Convert the value of variable text to lowercase using the built-in function strtolower() and assign it to the variable str

Step 2: Assign value 0 into the variables countV and countC to store the count of vowels and consonants

Step 3: Find the length of the variable text and assign it to the variable len

Step 4: Assign the value 0 into the variable i and perform the following sub-steps until the condition 'i < len' becomes false and increment the value of i by 1 in every iteration

        (i) Check the condition 'str[i]' is equal to any of 'a', 'e, 'i, 'o, 'u' it true add 1 with the value of variable countV and assign it to the variable countV other wise goto sub-step ii

        (ii) Check the condition 'str[i] >= "a" && str[i] <= "z"' if true add 1 with the value of variable countC and assign it to the variable countC

        (iii) If both the above sub-steps are false then do nothing and perform next iteration

Step 5: Print the value of variables countV and countC as the number of vowels and consonants

PHP Source Code

                                          <?php
function countVowelAndConsonants($text)
{
    $str = strtolower($text);
    $countV = 0;
    $countC = 0;
    $len = strlen($text);
    for ($i = 0; $i < $len; $i++) {
        if ($str[$i] == 'a' || $str[$i] == 'e' || $str[$i] == 'i' || $str[$i] == 'o' || $str[$i] == 'u') {
            $countV += 1;
        } else if ($str[$i] >= 'a' && $str[$i] <= 'z') {
            $countC += 1;
        } else {
        }
    }
    echo "Number of Vowels is $countV \n";
    echo "Number of Consonants is $countC ";
}

$text = readline("Enter the string: ");
countVowelAndConsonants($text);
?>
                                      

OUTPUT

Enter the string: You will learn PHP here
Number of Vowels is 7
Number of Consonants is 12