mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-29 04:57:33 +01:00
function for count vowels, consonant and numeric number from a string
This commit is contained in:
parent
f265ea78d2
commit
1e9ee9c73f
50
String/CountVowelsConsonantNumber.php
Normal file
50
String/CountVowelsConsonantNumber.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This function returns
|
||||
* the Total number of vowels total
|
||||
* number of consonant and total
|
||||
* numeric number present in the
|
||||
* given string using a simple
|
||||
* method of looping through all
|
||||
* the characters present in
|
||||
* the string.
|
||||
*
|
||||
* @param string $string
|
||||
*
|
||||
*/
|
||||
function countVowelsConsonant(string $string): String
|
||||
{
|
||||
if (empty($string)) {
|
||||
throw new \Exception('Please pass a non-empty string value');
|
||||
}
|
||||
|
||||
$vowels = ['a', 'e', 'i', 'o', 'u']; // Vowels Set
|
||||
$string = strtolower($string); // For case-insensitive checking
|
||||
|
||||
$vowelCount = 0;
|
||||
$consonantCount = 0;
|
||||
$numberCount = 0;
|
||||
|
||||
for ($i = 0; $i < strlen($string); $i++) {
|
||||
|
||||
if (in_array($string[$i], $vowels)) {
|
||||
|
||||
$vowelCount++;
|
||||
} elseif ($string[$i] >= 'a' && $string[$i] <= 'z') {
|
||||
|
||||
$consonantCount++;
|
||||
} elseif ($string[$i] >= 0 && $string[$i] <= 9 && is_numeric($string[$i])) {
|
||||
|
||||
$numberCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return 'total vowels - ' . $vowelCount . PHP_EOL .
|
||||
'total consonant - ' . $consonantCount . PHP_EOL .
|
||||
'total numeric number -' . $numberCount . PHP_EOL;
|
||||
}
|
||||
|
||||
// example
|
||||
echo countVowelsConsonant('Hello World. 12345');
|
||||
echo countVowelsConsonant('Count Vowels, Consonant and numeric number. 09876');
|
Loading…
x
Reference in New Issue
Block a user