Assignment No. 3 : To study functions & strings
SET A
Q: 1) Write a script to accept two integers(Use html form having 2 textboxes).
Write a PHP script to,
a. Find mod of the two numbers.
b. Find the power of first number raised to the second.
c. Find the sum of first n numbers (considering first number as n)
d. Find the factorial of second number.
(Write separate function for each of the above operations.)
<html>
<body>
<form action="Ass3.1.php" method="GET">
Number 1:<input type="number"name="no1"><br>
Number 2:<input type="number"name="no2"><br>
<p> which operation you want to perfrom </p>
<input type="radio"name="op" value="1">
<label for="mod">mod of the two no</label><br>
<input type="radio"name="op"value="2">
<label for="power">power of 1st no.rised to the 2nd</label><br>
<input type="radio" name="op" value="3">
<label for="sum">sum of 1st n no.</label><br>
<input type="radio"name="op" value="4">
<label for="factorial">factorial of 2nd no.</label><br>
<input type="submit">
</form>
</body>
</html>
Q: 2) Write a PHP script for the following: Design a form to accept a string.
a. Write a function to find the length of given string without using built in functions.
b. Write a function to count the total number of vowels i.e. (a,e,i,o,u) ) from the string.
c. Convert the given string to lowercase and then to Title case.
d. Pad the given string with “*” from left and right both the sides.
e. Remove the leading whitespaces from the given string.
f. Find the reverse of given string.(use built-in functions)
(Use radio buttons and the concept of function. Use ‘include’ construct or ‘require’ stmt.)
<html>
<body>
<form action="Slip_15.php" method="GET">
string1:<input type="text"name="txt1"><br>
<p>Which operation you want to perform </p>
<input type="radio" name="op" value="1">
<lable for="length"> find length of given string</lable><br>
<input type="radio" name="op"value="2">
<label for="vowel"> count total no.of vowels</label><br>
<input type="radio"name="op" value="3">
<label for="lowercase">convert total no.of lowercase</label><br>
<input type="radio"name="op" value="4">
<label for="pad">pad the given string with "*" from left & right </label><br>
<input type="radio"name="op" value="5">
<label for="whitespaces">Remove the white spaces from the given string </label><br>
<input type="radio"name="op" value="6">
<label for="reverse">Find reverse of given string</label><br>
<input type="submit">
</form>
</body>
</html>
Slip_15.php
<?php
$a=$_GET['txt1'];
$ch=$_GET['op'];
switch($ch)
{
case 1 :length($a);
break;
case 2 :count($a);
break;
case 3 :lowercase($a);
break;
case 4 :padboth($a);
break;
case 5 :rmwhite($a);
break;
case 6 :reverse($a);
break;
}
function length($a)
{
$b=strlen($a);
echo $b;
}
function countv($a)
{
$string = strlower($a);
vowel=array('a','e','i','o','u');
$len=strlen($string);
$num=0;
for($i=0;$i<$len;$i++)
{
if(in_array)$string[$i],$vowels)
{
$num++;
}
}
echo "Number of vowels;".$nu7m;
}
funtion lowercase($a)
{
$b=strtolower($a);
echo $b;
}
function padboth($a)
{
echo str_pad($a,20,"*",STR_PAD_BOTH);
}
function rmwhite($a)
{
echo ltrim($a);
}
function reverse($a)
{
echo strrev($a);
}
?>
SET B
Q. 1) :Write a PHP script for the following: Design a form to accept two strings from the user.
a. Find whether the small string appears at the start of the large string.
b. Find the position of the small string in the big string.
c. Compare both the string for first n characters, also the comparison should not be case sensitive.
Q. 2) : Write a PHP script for the following: Design a form having a text box and a drop down list containing any 3 separators(e.g. #, |, %, @, ! or comma) accept a strings from the user and also a separator.
a. Split the string into separate words using the given separator.
b. Replace all the occurrences of separator in the given string with some other separator.
c. Find the last word in the given string(Use strrstr() function).
HTML File:
<html>
<body>
<form action='ass1b2' method=post>
<pre>
Enter first string :<input type='text' name='str1'><br>
Enter second string :<input type='text' name='str2'><br>
Enter string for replace:<input type='text' name='str3'><br></h2>
<input type='radio' name="ch" value=1>Occurence.
<input type='radio' name="ch" value=2>Replace.
<input type='radio' name="ch" value=3>split.
<input type=submit value=ok> <input type=reset value=cancel>
</pre>
</form>
</body>
</html>
PHP Function:
NOTE: PHP function is saved as "ass1b2.php"
<?php
$str1=$_POST['str1'];
$str2=$_POST['str2'];
$str3=$_POST['str3'];
$ch=$_POST['ch'];
echo"First string=$str1.<br>";
echo"Second String=$str2.<br>";
echo"String for replace=$str3.<br>";
if(strlen($str1)>strlen($str2))
{
switch($ch)
{
case 1: $pos=strpos($str1,$str2);
if($pos!=0)
echo"String '$str2' Not present at the start of '$str1'.<br>";
else
echo"String '$str2' Present at the strat of '$str1'.<br>";
break;
case 2:
$str4=str_replace($str2,$str3,$str1);
printf("\nAfter replacing string::");
echo $str4;
break;
case 3: $s=preg_split("//",$str1);
foreach($s as $v) echo "\t$v <br>";
break;
}
}
else
{
switch($ch)
{
case 1:$pos=strpos($str2,$str1);
if($pos!=0)
echo"String '$str1' Not present at the start of '$str2'.<br>";
else
echo"String '$str1' Present at the start of '$str2'.<br>";
break;
case 2: $str4=str_replace($str1,$str3,$str2);
echo "After replacing string::$str4<br>";
break;
case 3:
echo"After splitting the string::<br>";
$s=preg_split("//",$str2);
foreach($s as $v) echo "\t$v <br>";
}
}
?>
Q. 3) : Write a PHP script having 3 textboxes. The first textbox should be for accepting name of the student, second for accepting name of college and third for accepting a proper greeting message. Write a function for accepting all the three parameters and generating a proper greeting message. If any of the parameters are not passed, generate the proper greeting message.
(Use the concept of missing parameters)
SET C
Q: 1) Write a PHP script for the following: Design a form to accept the marks of 5 different subjects of a student, havingserial number, subject name & marks out of 100. Display the result in the tabular format which will have total, percentage and grade. Use only 3 text boxes.(Use array of form parameters)
<html>
<form action="ass3c1.php" method="get">
<body>
Roll No :<input type="number" name="rollno"><br><br>
Name : <input type="text" name="name"><br><br>
Mark1 : <input type="text" name="mrk1"><br><br>
Mark2 : <input type="text" name="mrk2"><br><br>
Mark3 : <input type="text" name="mrk3"><br><br>
<input type="submit" name="submit">
</body>
</form>
</html>
"ass3c1.php
<?php
if(isset($_GET['submit']))
{
$rollno=$_GET['rollno'];
$name=$_GET['name'];
$mark1 = $_GET['mrk1'];
$mark2 = $_GET['mrk2'];
$mark3 = $_GET['mrk3'];
$total = $mark1+$mark2+$mark3;
$per = ($total/300)*100;
if($per>=75)
{
$grade = "Distinction";
}
else if($per>=60 && $per<75)
{
$grade = "First Class";
}
else if($per>=50 && $per<60)
{
$grade = "Second Class";
}
else
{
$grade = "Pass Class";
}
?>
<html>
<table border = "2">
<tr>
<th><b>Roll No</b></th>
<th><b>Name</b></th>
<th><b>Mark1</b></th>
<th><b>Mark2</b></th>
<th><b>Mark3</b></th>
<th><b>Total</b></th>
<th><b>Percentage</b></th>
<th><b>Grade</b></th>
</tr>
<tr>
<td><?php echo $rollno?></td>
<td><?php echo $name?></td>
<td><?php echo $mark1?></td>
<td><?php echo $mark2?></td>
<td><?php echo $mark3?></td>
<td><?php echo $total?></td>
<td><?php echo $per?></td>
<td><?php echo $grade?></td>
</tr>
</table>
<?php } ?>
Q: 2) Write a PHP script to accept 2 strings from the user, the first string should be a sentence and second can be a word.
a. Delete a small part from first string after accepting position and number of characters to remove.
b. Insert the given small string in the given big string at specified position without removing any characters from the big string.
c. Replace some characters/ words from given big string with the given small string at specified position.
d. Replace all the characters from the big string with the small string after a specified position.
(Use substr_replace() function)
Q: 3)Using regular expressions check for the validity of entered email-id.
a. The @ symbol should not appear more than once.
b. The dot (.) can appear at the most once before @ and at the most twice or at least once after @ symbol.
c. The substring before @ should not begin with a digit or underscore or dot or @ or any other special character. (Use explode and ereg function.)
No comments:
Post a Comment