HERE I HAVE SHOWN HOW TO FIND THE VOWEL AND CONSONANTS OF A STRING "FAREWELL TO OUR CAPTAIN COOL MS DHONI FROM IPL AS A CAPTAIN" USING PHP STRING FUNCTIONS(HERE I HAVE EXPLAINED PROPERLY EACH AND EVERY LINE OF THE CODE WITH MY OWN EXPLANATION, SO THAT IT CLEARS YOUR CONCEPTS)
HERE I HAVE SHOWN HOW TO FIND THE VOWEL AND CONSONANTS OF A STRING "FAREWELL TO OUR CAPTAIN COOL MS DHONI FROM IPL AS A CAPTAIN" USING PHP STRING FUNCTIONS(HERE I HAVE EXPLAINED PROPERLY EACH AND EVERY LINE OF THE CODE WITH MY OWN EXPLANATION, SO THAT IT CLEARS YOUR CONCEPTS)
Hi All, Very good morning. Today here in my blog I have shown how to find the vowels and consonants from a string. So, withing wasting time, let's begin with my explanation of how to find the vowels and consonants from a string.
Before starting I just want to explain about what is string? So, guy's, I hope you all are aware of what is string? String is nothing, but it is a suquence of characters which is denoted either in double quotation mark("") or in single quotation mark(''). Like for example "Hello World!" or 'Hello World!'.
So guy's, now let's begin with my explanation of how to find vowels and consonants from a string. So firstly, I have taken the string " FAREWELL TO OUR CAPTAIN COOL MS DHONI FROM IPL AS A CAPTAIN" within one variable which variable I have named as $str and then I have taken another two variables one is $vow for counting the vowels and $cons for counting the consonants and both $vow and $cons, I have declared as 0. Then I have replace the spaces of a string with without using the spaces by using str_replace() function as because I will not count the spaces within a string to find the vowels and consonants. Now you may ask me a question what is this str_replace(0 function called? Very good question. Ok, let me explain about the function of str_replace() funnction. str_replace() function is a function which replace the character with some other characters in a string. For example: like here I have replace the spaces with not having spaces. And then I have taken another variable $length where I have counted the length of a string, and to count the length of a string I have used the function strlen() function. So guy's, now again you may ask me a question what is strlen() function? It's another good question. Yes guy's , strlen() function is a function which counts the length of a string. Now I have used for loop where I have taken another variable $i and I have declared that variable $i as 0 and then I have used $i<$length, $i++. So, basically here I am running the for loop which means the loop will run until or unless $i<length, till then my loop will run and $i will get incremented by 1 till the loop runs. Then I have used if condition where "if ($str[$i] == 'A' || $str[$i] == 'E' || $str[$i] == 'I' || $str[$i] == 'O' || $str[$i] == 'U' || $str[$i] == 'a' || $str[$i] == 'e' || $str[$i] == 'i' || $str[$i] == 'o' || $str[$i] == 'u')" and if the if condition satisfies then the vowels will get incremented by 1 and the vowels will get printed. And if the if condition do not satisfy then it will go into my else part where the consonants will get incremented by 1 and the consonants will get printed. Then I have just printed the total vowels and total consonants in a string using print_r() function.
CODE =>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MS DHONI</title>
<style type="text/css">
.body-group
{
text-align: center;
color: darkgreen;
}
</style>
</head>
<body class="body-group">
<h1>FAREWELL TO OUR CAPTAIN COOL MS DHONI FROM IPL AS A CAPTAIN</h1>
<img src="https://akm-img-a-in.tosshub.com/indiatoday/images/story/202505/ms-dhoni-203452473-16x9_0.jpeg?VersionId=opvRqUulDYGvFzKEuG7mWO1D0BQ_6SIe&size=690:388" height="250px" width="250px">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRKjOPOIx7HjlRFOToI_sPjihEtkm6mWlyfnQ&s" height="250px" width="250px">
<img src="https://static.toiimg.com/thumb/msid-121279771,imgsize-39846,width-400,resizemode-4/121279771.jpg" height="250px" width="250px">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR8pVED9I_utMTg47mUlKu9K17FUmRJ1ewmsA&s" height="250px" width="250px">
<img src="https://www.hindustantimes.com/ht-img/img/2025/05/21/550x309/dhoniba_1747808878331_1747808892825.jpg" height="250px" width="250px">
<h3>Respected Dhoni Sir, whether you retire today as a captain from IPL or not, it doesn't matter to me. But as a captain cool, handle each and every situation in a very cool mind state as a Captain starting from 2008 to 2025, for the last 17 years. Just standing behind the wickets, you are just incredible and also with a bat. So, whether you retire as a captain on today after with Gujarat Titan's match or not, you will always remain to me as a captain cool and a learner to me who holds all 5 IPL tropies alone as a captain.</h3>
</body>
</html>
<?php
$str="FAREWELL TO OUR CAPTAIN COOL MS DHONI FROM IPL AS A CAPTAIN";
$vow=0;$cons=0;
$str=str_replace(" ", "", $str);
$length=strlen($str);
for ($i=0; $i < $length; $i++) {
if ($str[$i] == 'A' || $str[$i] == 'E' || $str[$i] == 'I' || $str[$i] == 'O' || $str[$i] == 'U' || $str[$i] == 'a' || $str[$i] == 'e' || $str[$i] == 'i' || $str[$i] == 'o' || $str[$i] == 'u') {
$vow++;
}
else{
$cons++;
}
}
print_r("The vowel of the string is: ".$vow."<br>");
print_r("The consonants of the string is: ".$cons."<br>");
?>
Comments
Post a Comment