PHP Program to Count Vowels in a String



A string is a sequence of characters, including alphabets, numbers, and symbols. In this tutorial, we are going to learn how we can count the number of vowels in a given string in PHP using different approaches. Vowels in English are a, e, i, o, u, and they can be either uppercase or lowercase.

What is a Vowel?

Vowels are alphabetic characters that represent specific speech sounds.
There are a total of five vowels, both uppercase and lowercase in the English language:

a, e, i, o, u

Example 1

  • Input: string = "Tutoriaspoint"
  • Output: 6

Explanation

The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. Which are a total of 6 vowels.

Example 2

  • Input: string = "PHP"
  • Output: 0

Explanation

The string "PHP" does not contain any vowels, so the count is 0.

Example 3

  • Input: string = "Ayush Mishra"
  • Output: 4

Explanation

The vowels in the string are a, u, i, a. There are a total of 4 vowels.

Below are different approaches to counting vowels in a string using PHP:

  • Using Direct Logic Approach
  • Using a Function

Count Vowels in a String Using Direct Logic Approach

We use a simple loop to traverse the string and check if each character is a vowel. If it is, we increment the counter. After the loop ends, we return the total count of vowels.

Steps for Implementation

  1. First, define a string as the input parameter.
  2. Now, initialize a count variable to 0.
  3. Loop through each character in the string. 
  4. For each character, check if it is a vowel (case-insensitive).
  5. Increase the count variable for each vowel.
  6. Return the count of vowels.

Implementation Code

<?php

$string = "Anshu Ayush";
$vowel_count = 0;

// Convert string to lowercase
$string = strtolower($string);

// Loop through each character in the string
for ($i = 0; $i < strlen($string); $i++) {
    if (in_array($string[$i], ['a', 'e', 'i', 'o', 'u'])) {
        $vowel_count++;
    }
}

// Output
echo "The number of vowels in the string '$string' is: $vowel_count";

?>

Output

The number of vowels in the string 'anshu ayush' is: 3

Time Complexity: O(n)
Space Complexity: O(1)

Count Vowels in a String Using Function

In this approach, we use a function to count the number of vowels in a string. We use function so that we can later use function whenever necessary.

Steps for Implementation

  1. First, create a function that accepts a string as an input.
  2. Now, initialize a count variable to 0 and convert the string to lowercase.
  3. Traverse the string and check for vowels using a predefined list.
  4. Increase the count variable for each vowel.
  5. Return the count of vowels.

Implementation Code

<?php
// Function to count vowels
function count_vowels($string) {
    $vowel_count = 0;
    $string = strtolower($string); 
    for ($i = 0; $i < strlen($string); $i++) {
        if (in_array($string[$i], ['a', 'e', 'i', 'o', 'u'])) {
            $vowel_count++;
        }
    }
    return $vowel_count;
}

$string = "Anshu Ayush";
$vowel_count = count_vowels($string);

echo "The number of vowels in the string '$string' is: $vowel_count";
?>

Output

The number of vowels in the string 'Anshu Ayush' is: 4

Time Complexity: O(n)
Space Complexity: O(1)

Updated on: 2025-01-15T18:47:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements