0% found this document useful (0 votes)
9 views20 pages

String Set-1 - : Code

The document contains a series of PHP lab exercises focused on string manipulation, loops, and functions. It includes scripts for tasks such as splitting strings, checking for substrings, reversing numbers, and generating prime numbers. Each section provides code examples and expected outputs for various programming challenges.

Uploaded by

vexijo2666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

String Set-1 - : Code

The document contains a series of PHP lab exercises focused on string manipulation, loops, and functions. It includes scripts for tasks such as splitting strings, checking for substrings, reversing numbers, and generating prime numbers. Each section provides code examples and expected outputs for various programming challenges.

Uploaded by

vexijo2666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Web Application Development CSE(AIML)-C4

Lab 15
----------------String Set-1-----------------

Q1. Write a PHP script to split following string. Sample


input='180425',Expected Output='18-04-25'.
Q2. Write PHP script to extract username from following
email ID
Q3. Write a PHP script to check whether a string
contains a specific string. Sample String :Adani
University contains 'university' in it or not.
Q4. Write a PHP script to find the first character that is
different b/w the two string.
string1 = bad
string2 = bed
expected output = "the first character that is different
is at position 2.

Code:
<!DOCTYPE html>
<html>
<head>
<title>Set1</title>
</head>
<body>
<h1>Set 1 Questions</h1>
<?php
//Q1
echo "<b>Write a PHP script to split the following string.</b><br>";
$str = "06052025";
$new_str = substr($str, 0, 2) . '-' . substr($str, 2, 2) . '-' . substr($str, 4, 4);
echo "Date: ".$new_str;

//Q2
echo "<br><br>";
echo "<b>Write a PHP script to extract the user name from the following email
ID.</b><br>";
$email = "[email protected]";
$Pos = strpos($email, "@");
$username = substr($email, 0, $Pos);
echo "Username: " . $username;

Enroll.No:220148 Rollno:219 pg. 1


Web Application Development CSE(AIML)-C4

//Q3
echo "<br><br>";
echo "<b>Write a PHP script to check whether a string contains a specific string? Check
whether the above string contains the string 'university'.</b><br>";
$str = "I am in Adani University";
$lowerstr = strtolower($str);
$target = "university";
echo "String is: ";
echo "$str";
echo "<br>";
if (strpos($lowerstr, $target) == true) {
echo "string contains 'university'";
} else {
echo "string does not contain 'university'.";
}

//Q.4
echo "<br><br>";
echo "<b>Write a PHP script to find the first character that is different between two
strings. str1: 'bad' str2: 'bed'.</b><br>";
$str1 = "bad";
$str2 = "bed";
$length1 = strlen($str1);
$length2 = strlen($str2);
if ($length1 < $length2) {
$limit = $length1;
} else {
$limit = $length2;
}
$found = false;
for ($i = 0; $i < $limit; $i++) {
if ($str1[$i] !== $str2[$i]) {
echo "First difference at index $i: '{$str1[$i]}' vs '{$str2[$i]}'";
$found = true;
break;
}
}
if (!$found) {
if ($length1 != $length2) {
echo "Strings are equal up to $limit characters, but lengths differ.";
} else {
echo "The strings are identical.";
}
}
?>
</body>
</html>

Enroll.No:220148 Rollno:219 pg. 2


Web Application Development CSE(AIML)-C4

Output:

Enroll.No:220148 Rollno:219 pg. 3


Web Application Development CSE(AIML)-C4

Lab 16
----------------String Set-2-----------------
Q1. Write a PHP script to get the absolute difference
between x and 50 . If X is greater than 50, then print 4
times the absolute difference.
Q2. Write a PHP script to remove the character from the
given position from a string.
Q3. Interchange first and last digits of a given number.
Q4. Write a PHP script to insert a character at a
specified position.
Q5. Write a PHP script to Find and Replace.

Code:

<!DOCTYPE html>
<html>
<head>
<title>Set2</title>
</head>
<body>
<h1>Set 2 PHP Questions</h1>
<?php
//Q1
echo "<b> Write a php program to get the abosolute difference x and 50. If x is greater than 50
return triple the absolute difference.</b><br>";
$x = 57;
$result = abs($x - 50);
if ($x > 50) {
$result = 3 * $result;
}
echo "The absolute difference is: $result<br>";

//Q2
echo "<br><b>Write a PHP script to remove the character from the given position from a
string.</b><br>";
$s1 = "Hello";
$i = 2;
if ($i > strlen($s1)) {
echo "Index invalid<br>";
}
else {
$result1 = substr($s1, 0, $i).substr($s1, $i+1, strlen($s1)-$i);
echo "Original String: $s1<br>";
echo "Modified String: $result1<br>";
}

Enroll.No:220148 Rollno:219 pg. 4


Web Application Development CSE(AIML)-C4

// Q.3
echo "<br><b>Interchange first and last digits of a given number.</b><br>";
$number2 = 12345;
$numStr = (String)$number2;
$length = strlen($numStr);
if ($length < 2) {
echo "Result: " . $number;
} else {
$first = $numStr[0];
$last = $numStr[$length - 1];
$numStr[0] = $last;
$numStr[$length - 1] = $first;

echo "Original Number: $number2\n";


echo "<br>Modified Number: " . $numStr;
}

//Q4
echo "<br><br><b>Write a PHP script to insert a character at a specified position.</b><br>";
$s2 = "JavaScript";
$ch = "b";
$pos = 3;
if ($pos > strlen($s2)) {
echo "Index invalid<br>";
}
$result2 = substr($s2, 0, $pos).$ch.substr($s2, $pos, strlen($s2)-$pos);
echo "Original String: $s2<br>";
echo "Modified String: $result2<br>";

//Q5
echo "<br><b>Write a PHP script to Find and Replace.</b><br>";
$string4 = "Hello World!";
$find = "World";
$replace = "PHP";
$position = strpos($string4, $find);
if ($position !== false) {
$newString = substr($string4, 0, $position) . $replace . substr($string4, $position +
strlen($find));
echo "Original String: $string4\n";
echo "<br>Modified String: $newString\n";
} else {
echo "Substring not found.";
}

?>
</body>
</html>

Enroll.No:220148 Rollno:219 pg. 5


Web Application Development CSE(AIML)-C4

Output:

Enroll.No:220148 Rollno:219 pg. 6


Web Application Development CSE(AIML)-C4

Lab 17
---------------- LOOPS-----------------
Q1. Write a PHP program to compute the sum of the two
reversed numbers and display the sum in reversed form.
Input: 13,14
Output: 72
Q2. Write a PHP program which iterates the integers
from 1 to 30. For multiples of three print "three" instead
of the number and for the multiples of five print "five".
For numbers which are multiples of both three and five
print "threefive".
Q3. Write a PHP program to check two given integers,
each in the range 10..99. Return true if a digit appears
in both numbers, such as the 5 in 15 and 51.
Q4. Check that the given n digit number is palindrome
or not (Without Using PHP Predefine Function).
Q5. PHP script for generating a list of prime numbers
below 100.
Q6. Reversing Number
Q7. Write a PHP program to reverse the digits of an
integer.
Q8. Write a PHP script using nested for loop that creates
a chess board as shown below.Use table width="270px"
and take 30px as cell height and width.
Q9. Write a PHP script to create multiplication table
from 1 to 10 with cell spacing 5px and cellpadding 5px.

Code:

<!DOCTYPE html>
<html>
<head>
<title>Set3</title>
</head>
<body>
<h1>Set 3 PHP Questions</h1>
<?php

Enroll.No:220148 Rollno:219 pg. 7


Web Application Development CSE(AIML)-C4

// Q.1
echo "<b>Write a PHP program to compute the sum of the two reversed numbers and
display the sum in reversed form.</b><br>";

$num1 = 13;
$num2 = 14;
$rev1 = (int)strrev((string)$num1);
$rev2 = (int)strrev((string)$num2);
$sum = $rev1 + $rev2;
$result = strrev((string)$sum);

echo "Input: $num1, $num2 <br>";


echo "Output: $result <br><br>";

// Q.2
echo "<b>Write a PHP program which iterates the integer from 1 to 30. For multiples of
three print 'three' instead of the number and for the multiples of five print 'five'. For numbers
which are multiples of both three and five print 'threefive'.</b><br>";
for ($i = 1; $i <= 30; $i++) {
if ($i % 3 == 0 && $i % 5 == 0) {
echo "threefive ";
} elseif ($i % 3 == 0) {
echo "three ";
} elseif ($i % 5 == 0) {
echo "five ";
} else {
echo $i . " ";
}
}

// Q.3
echo "<br><br><b>Write a PHP program to check two given integers, each in the range
10to99. Return true if a digit appears in both numbers, such as the 5 in 15 and 51.</b><br>";
$x = "15";
$y = "51";
$n = "5";
if (($x[0] == $n || $x[1] == $n) && ($y[0] == $n || $y[1] == $n)) {
echo "The digit appears in both the numbers<br";
}
else {
echo "The digit does not appear in both the numbers<br>";
}

// Q.4
echo "<br><br><br><b>Check that the given n digit number is palindrome or
not</b><br>";
$num = 121;
echo "Number is $num <br>";
$str = (string) $num;

Enroll.No:220148 Rollno:219 pg. 8


Web Application Development CSE(AIML)-C4

$numlen = strlen($str);
$isPalindrome = true;

for ($i = 0; $i < $numlen / 2; $i++) {


if ($str[$i] != $str[$numlen - 1 - $i]) {
echo "$num is not a palindrome";
$isPalindrome = false;
break;
}
}
if ($isPalindrome) {
echo "$num is a palindrome number<br>";
}

// Q.5
echo "<br><b>PHP script for generating a list of prime numbers below 100.</b><br>";
for ($i = 2; $i < 100; $i++) {
$isPrime = true;
for ($j = 2; $j <= sqrt($i); $j++) {
if ($i % $j == 0) {
$isPrime = false;
break;
}
}
if ($isPrime) {
echo "$i ";
}
}

// Q.6
echo "<br><br><br><b>Write a php program Reversing Number</b><br>";
$num = 12345;
$rev = strrev((string) $num);

echo "Original Number: $num";


echo "Reverse number : $rev";

//Q.7
echo "<br><br><b>Write a PHP program to reverse the digits of an integer.</b><br>";
$num = 12345;
$rev = 0;
while ($num >0){
$x = $num % 10;
$rev = $rev * 10 + $x;
$num = (int)($num / 10);
}

Enroll.No:220148 Rollno:219 pg. 9


Web Application Development CSE(AIML)-C4

echo "The reverse of the number is: $rev<br>";

// Q.8
echo "<br><b>Write a PHP script using nested for loop that creates a chess board as
shown below.Use table width=270px and take 30px as cell height and width.</b><br>";

echo "<table width='270px'>";


for ($i = 0; $i<8; $i++) {
echo "<tr>";
for ($j = 0; $j < 8; $j++) {
if (($i + $j) % 2 == 0) {
echo "<td style='height:30px;width:30px;background-color:white'></td>";
} else {
echo "<td style='height:30px;width:30px;background-color:black'></td>";
}
}
echo "</tr>";
}
echo "</table>";

// Q.9
echo "<br><b>Write a PHP script to create multiplication table from 1 to 10 with cell
spacing 5px and cellpadding 5px.</b><br>";
echo "<table cellspacing='5' cellpadding='5'>";
for ( $i = 1; $i <= 10; $i++) {
echo "<tr>";
for ($j = 1; $j <= 10; $j++) {
echo "<td>" .$i. "*". $j . "=" .($i * $j) . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

Enroll.No:220148 Rollno:219 pg. 10


Web Application Development CSE(AIML)-C4

Output:

Enroll.No:220148 Rollno:219 pg. 11


Web Application Development CSE(AIML)-C4

Lab 17
---------------- Functions-----------------
Q1. Write a PHP function that checks whether a string is
all lowercase. (Ord () gives ASCII value of the character )
Q2. Write a PHP function that checks whether a passed
string is a palindrome or not ?
Q3. Write a PHP function to add the digits of a positive
integer repeatedly until the result has a single digit. For
example given number is 59 , the result is 5.
Step1: 5 + 9 = 14
Step 2 : 1 + 4 = 5
Input : 48 Output : 3

Code:
<!DOCTYPE html>
<html>
<head>
<title>Set4</title>
</head>
<body>
<h1>Set 4 PHP Questions</h1>
<?php
// Q.1
echo "<b>Write a php fucntion to check whether the string is all lowercase</b><br>";
function islowercase($s) {
$flag = true;
for ($i = 0; $i < strlen($s); $i++) {
if (ord($s[$i]) >= 65 && ord($s[$i]) <= 90) {
$flag = false;
}
}
if ($flag) {
echo "$s is Contain all lowercase<br>";
}
else {
echo "$s is not Contain all lowercase<br>";
}
}
islowercase("Yash");
islowercase("yash");

// Q.2

Enroll.No:220148 Rollno:219 pg. 12


Web Application Development CSE(AIML)-C4

echo "<br><br><b>Write a PHP function that checks whether a passed string is a


palindrome or not ?</b><br>";
function strPalindrome($str) {
$str = strtolower($str);
$revstr = "";
for ($i = strlen($str)-1; $i >= 0; $i--) {
$revstr = $revstr.$str[$i];
}

if ($revstr == $str) {
echo "$str is a palindrome<br>";
}
else {
echo "$str is not a palindrome<br>";
}
}
strPalindrome("Jalaj");
strPalindrome("Yash");

// Write a php function to add the digits of a positive integer repeatedly until the result
has a single digit
echo "<br><br><b>Write a PHP function to add the digits of a positive integer
repeatedly until the result has a single digit. For example given number is 59 , the result is
5.</b><br>";
function sumdigits($num) {
if ($num <= 0) {
echo "The number should be greater than 0<br>";
return;
}
else {
$sum = 0;
while ($num > 0) {
$rem = $num % 10;
$sum = $sum + $rem;
$num = (int) ($num /10);
}
if ($sum > 9) {
return sumdigits($sum);
}
else {
return $sum;
}
}
}
echo "The sum of number until single digit: ".sumdigits(59)."<br>";
echo "The sum of number until single digit: ".sumdigits(48)."<br>";
?>
</body>
</html>

Enroll.No:220148 Rollno:219 pg. 13


Web Application Development CSE(AIML)-C4

Output:

Enroll.No:220148 Rollno:219 pg. 14


Web Application Development CSE(AIML)-C4

Lab 18
---------------- Array-----------------
Q1. Write a PHP function to find three numbers
from an array such that the sum of three
consecutive numbers equal to zero.

Q2. Write a PHP program to find a single number


in an array that doesn't occur twice.

Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Array</title>
</head>
<body>
<?php

//Q.1
echo "<b>Write a PHP function to find three numbers from an array such that the sum
of three consecutive numbers equal to zero.</b><br>";
function Sum($arr) {
$n = count($arr);
for ($i = 0; $i <= $n - 3; $i++) {
$sum = $arr[$i] + $arr[$i+1] + $arr[$i+2];
if ($sum == 0) {
echo "Three consecutive numbers with sum 0 are: {$arr[$i]}, {$arr[$i+1]},
{$arr[$i+2]}<br>";
return;
}
}
echo "No three consecutive numbers found with sum 0.";
}
$arr = [4, -3, -1, 2, -2, 0, 1];
Sum($arr);

//Q.2
echo "<br><b>Write a PHP program to find a single number in an array that doesn't
occur twice.</b><br>";

function single($arr) {

Enroll.No:220148 Rollno:219 pg. 15


Web Application Development CSE(AIML)-C4

$arr2 = [];
$n = count($arr);

for ($i = 0; $i < $n; $i++) {


$unique = true;
for ($j = 0; $j < count($arr2); $j++) {
if ($arr[$i] == $arr2[$j]) {
$unique = false;
break;
}
}
if ($unique) {
$arr2[] = $arr[$i];
}
}

for ($i = 0; $i < count($arr2); $i++) {


$count = 0;
for ($j = 0; $j < $n; $j++) {
if ($arr[$j] == $arr2[$i]) {
$count++;
}
}
if ($count == 1) {
return $arr2[$i];
}
}

return "No unique number";


}

$arr = [1, 2, 3, 2, 1];


echo "The number that doesn't occur twice is: " . single($arr);
?>
</body>
</html>
Output:

Enroll.No:220148 Rollno:219 pg. 16


Web Application Development CSE(AIML)-C4

Lab 19
---------------- Form and database connection
-----------------
Q1. Make a form and submit the data to the connected sql
database and then display the Data.
Code:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$host = "localhost";
$user = "root";
$pass = "";
$db = "practical";

$conn = new mysqli($host, $user, $pass, $db);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Get form data safely


$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$gender = $_POST['gender'];
$city = $_POST['city'];

// Insert query with quotes around values


$sql = "INSERT INTO users (name, email, phone, gender, city)
VALUES ('$name', '$email', '$phone', '$gender', '$city')";

if ($conn->query($sql) === TRUE) {


echo "Registration successful!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
?>
<!DOCTYPE html>
<html lang="en">

Enroll.No:220148 Rollno:219 pg. 17


Web Application Development CSE(AIML)-C4

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form in Html css </title>
<style>
body { margin-top: 5px;
font-family: Arial, sans-serif; border: 1px solid #ccc;
background-color: #37279f; border-radius: 5px;
display: flex; }
justify-content: center;
align-items: center; .gender,
height: 100vh; .hobby {
margin: 0; display: flex;
} /* border: 1px solid black; */
width: 200px;
.container { align-items: center;
background: #ffffff; margin-left: 30px;
padding: 20px; }
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0, .hobby {
0, 0, 0.1); width: 300px;
width: 400px; }
}
.hobby label {
h2 { font-weight: normal;
text-align: center; }
color: #333;
} button {
width: 100%;
label { padding: 10px;
display: block; margin-top: 15px;
margin-top: 10px; border: none;
} border-radius: 5px;
background: #285ba7;
input, color: white;
select { font-size: 16px;
width: 100%; cursor: pointer;
padding: 8px; }
</style>
</head>

<body>

<div class="container">
<h2>Registration Form</h2>
<form action="#" method="post">
<label for="name">Name</label>
<input type="text" id="name" name="name" required>

<label for="email">Email</label>

Enroll.No:220148 Rollno:219 pg. 18


Web Application Development CSE(AIML)-C4

<input type="email" id="email" name="email" required>

<label for="phone">Phone No</label>


<input type="tel" id="phone" name="phone" required>

<label>Gender</label>
<div class="gender">
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label>
</div>

<label for="city">City</label>
<select id="city" name="city" required>
<option value="">Select City</option>
<option value="delhi" selected>Delhi</option>
<option value="mumbai">Mumbai</option>
<option value="bangalore">Bangalore</option>
<option value="chennai">Chennai</option>
</select>

<button type="submit">Register</button>
</form>
</div>

</body>

</html>

Enroll.No:220148 Rollno:219 pg. 19


Web Application Development CSE(AIML)-C4

Output:

Enroll.No:220148 Rollno:219 pg. 20

You might also like