Q1) Write a PHP script for the following: Design a form to accept a string. Write a function to count the total number of vowels (a,e,i,o,u) from the string. Show the occurrences of each vowel from the string.Check whether the given string is a palindrome or not, without using built-in function.(Use radio buttons and the concept of function.Use‘include’construct or require stmt.)
1.Create Slip1.php File
<?php
function cnt_vowels($a,$l)
{
$v_cnt = 0;
for($i=0;$i<$l;$i++)
{
if(($a[$i]=='a')||($a[$i]=='e')||($a[$i]=='i')||($a[$i]=='o')||
($a[$i]=='u')||($a[$i]=='A')||($a[$i]=='E')||($a[$i]=='I')||
($a[$i]=='O')||($a[$i]=='U'))$v_cnt++;
}
return $v_cnt;
}
function occured_vowelsinstring($a,$l)
{
$av=0;
$ev=0;
$iv=0;
$ov=0;
$uv=0;
$cnt=0;
for ($i = 0; $i < $l; $i++) {
if (($a[$i] == 'a') || ($a[$i] == 'A'))
{
$av++;
}
elseif (($a[$i] == 'e') || ($a[$i] == 'E'))
{
$ev++;
}
elseif (($a[$i] == 'i') || ($a[$i] == 'I'))
{
$iv++;
}
elseif (($a[$i] == 'o') || ($a[$i] == 'O'))
{
$ov++;
}
elseif (($a[$i] == 'u') || ($a[$i] == 'U'))
{
$uv++;
}
else
{
$cnt++;
}
}
echo"<br>Total'a':$av";
echo"<br>Total'e':$ev";
echo"<br>Total'i':$iv";
echo"<br>Total'o':$ov";
echo"<br>Total'u':$uv";
echo"<br>Total Count Without Vowels:$cnt";
$tot=$av+$ev+$iv+$ov+$uv;
return $tot;
}
$str=$_POST['str'];
$op=$_POST['op'];
$l=strlen($str);
switch($op)
{
case 1:
echo"String is=$str<br>";
$v_cnt=cnt_vowels($str,$l);
echo"Total vowels Present in string are =$v_cnt<br>";
exit(0);
case 2:
echo"string is=$str<br>";
$v_occ=occured_vowelsinstring($str,$l);
echo"<br>Total Vowels Present in String are=$v_occ<br>";
exit(0);
}
$str1=strrev($str);
$a=strlen($str);
$f=0;
for($j=0;$j<$a;$j++)
{
if($str1[$j]==$str[$j])
{
$f=0;
}
else
{
$f=1;
break;
}
}
if($f==0)
{
echo"String is Palindrome";
}
else
{
echo"String is Not Palindrome";
}
?>
2.Create slips.html
<html>
<body>
<form action="slip1.php" method="post">
<h3>Enter String:<input type="text"name="str" maxlength="20"></h3>
<input type="radio"name="op"value="1">Count of Total Vowels in String<br>
<input type="radio"name="op"value="2">Occurances of each vowel in string<br>
<input type="radio"name="op"value="3">Check Given String is Palindrome or Not<br><br>
<input type="submit"value="Click to See Output">
</form>
</body>
</html>
No comments:
Post a Comment