Home >Backend Development >PHP Tutorial >PHP Program to Count Vowels in a String

PHP Program to Count Vowels in a String

Susan Sarandon
Susan SarandonOriginal
2025-02-07 12:12:11875browse

PHP Program to Count Vowels in a String

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be capitalized. or lowercase.

What is a vowel?

Vowels are alphabetic characters representing specific pronunciations. There are five vowels in English, including caps and lowercase:

<code>a, e, i, o, u</code>

Example 1

  • Input: String = "Tutorialspoint"
  • Output: 6

Explanation

The vowels in the string "Tutorialspoint" are

u, o, i, i, a, o, i. There 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, i, a

. There are a total of

4

vowels.
  • The following are different ways to use PHP to count vowels in strings:
  • Use direct logic method

Using function

Use direct logic methods to count vowels in strings

We use a simple loop to iterate over the string and check if each character is a vowel. If so, increment the counter. After the loop ends, we return the total number of vowels.
  1. Implementation steps
  2. First, define a string as input parameter.
  3. Now, initialize the count variable to 0.
  4. Loop through each character in the string.
  5. For each character, check if it is a vowel (case insensitive).
  6. Add count variables for each vowel.

Returns the count of vowels.

<code class="language-php"><?php
$string = "Anshu Ayush";
$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++;
    }
}

// 输出
echo "字符串 '$string' 中元音的数量是:$vowel_count";

?></code>

Implementation code

<code>字符串 'anshu ayush' 中元音的数量是:3</code>

Output
Time complexity: O(n)

Space complexity:

O(1)

Use function to count vowels in strings In this method, we use a function to calculate the number of vowels in a string. We use a function so that we can use it later as needed.

Implementation steps

  1. First, create a function that accepts strings as input.
  2. Now, initialize the count variable to 0 and convert the string to lowercase.
  3. Transfer through the string and check vowels with a predefined list.
  4. Add count variables for each vowel.
  5. Returns the count of vowels.

Implementation code

<code>a, e, i, o, u</code>

Output

<code class="language-php"><?php
$string = "Anshu Ayush";
$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++;
    }
}

// 输出
echo "字符串 '$string' 中元音的数量是:$vowel_count";

?></code>

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

The above is the detailed content of PHP Program to Count Vowels in a String. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How do you parse and process HTML/XML in PHP?Next article:None