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

PHP Program to Count Vowels in a String

Susan Sarandon
Release: 2025-02-07 12:12:11
Original
919 people have browsed it

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>
Copy after login
Copy after login

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.

<?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";

?>
Copy after login
Copy after login

Implementation code

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

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>
Copy after login
Copy after login

Output

<?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";

?>
Copy after login
Copy after login

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!

Related labels:
php
Statement of this Website
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
Latest Articles by Author
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template