185. PHP Program to Swapping of two strings without using temporary variable using strings
How does this program work?
- In this programme you will learn about how to interchange the contents of two variables without using temperory variable using strings.
- Here we take only two varibles s1 and s2 and the content will be stored in these two variable.
- After that by using given logic we can get the output using PHP.
Here is the code
<html>
<head>
<title>PHP Program to Swapping of two strings without using temporary variable using strings</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="name1" value="" placeholder="Enter 1st string"/> </td>
</tr>
<tr>
<td> <input type="text" name="name2" value="" placeholder="Enter 2nd string"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit'])) {
$str1 = $_POST['name1'];
$str2 = $_POST['name2'];
echo "Strings before swapping:</br>" ;
echo "S1 = " .$str1."</br>";
echo "S2 = " . $str2."</br>";
//Concatenate both the string str1 and str2 and store it in str1
$str1 = $str1 . $str2;
//Extract str2 from updated str1
$str2 = substr($str1, 0, (strlen($str1) - strlen($str2)));
//Extract str1 from updated str2
$str1 = substr($str1, strlen($str2));
echo "Strings after swapping:</br>" ;
echo "S1 = " .$str1."</br>";
echo "S2 = " . $str2."</br>";
}
?>
</body>
</html>