₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,153 members, 8,420,584 topics. Date: Friday, 05 June 2026 at 04:34 AM

Toggle theme

[MOD] PHP WORKOUTS - String Manipulation 1 - Programming - Nairaland

Nairaland ForumScience/TechnologyProgramming[MOD] PHP WORKOUTS - String Manipulation 1 (6176 Views)

1 Reply (Go Down)

[MOD] PHP WORKOUTS - String Manipulation 1 by Kidstell(op):
Kudos to all readers and gurus Special regards to @Elvisten. While developing an algorithm to mix 2 or more strings. You are to write a code to mix these two strings thus.
Let $a="$_POST['string1']"; $b="$_GET['key']"; mix $a and $b so that we have
$answer="$_POST$[_'GsEtTr[i'nkge1y'']]";
if you are confused please you can change the values of $a and $b
your solution must work even if the values of both or one of $a and $b is not known.
A fairly simple solution can be found @ http://ideone.com/bjlzIK. Watch out for more workouts in the php gymnasium, hotter higher, harder-- what are you made of CLAY or CODE... Spit it now
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Elvisten10(m): 6:20pm On Sep 21, 2013
There is a php function for that:
<?php
function mixString($str1, $str2) {
return str_shuffle($str2.$str1);
}
echo mixString('Elvis', 'Chidera');
echo "\n";
echo mixString('Chidera', '');
?>
http://ideone.com/tf1d3d
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by adewasco2k(m):

<?php

/**
* @author adewasco2k
* @copyright 2013
*/

$a= "$`_POST['string1']";
$b= "$`_GET['key']";

//$answer="$_POST$[_'GsEtTr[i'nkge1y'']]";


$answer=""; //empty string to hold our answer

$x=0;

$strtest= strlen($a)>= strlen($b) ? $a : $b; //check which is longer in lenght

for($i=0;$i<strlen($strtest); $i++)
{
if($i<=6) // this is to get the first $_POST
{
$answer.=$a[$i];
}else // now we got $_POST lets mix the string
{
$answer.=$b[$x];
$answer.=$a[$i];
$x++;
}



}
echo $answer;

?>


Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Elvisten10(m): 10:32pm On Sep 23, 2013
^^^, i guess i did not read the question well and missed the i need your algo part. Here is my code for this:
<?php
function mixString($str1, $str2) {
$str = $str1.$str2;
$i = strlen($str);
$temp = '';
while($i > 1) {
$i = $i - 1;
$j = mt_rand(0, $i);
$temp = $str[$j];
$str[$j] = $str[$i];
$str[$i] = $temp;
}
return $str;
}
echo mixString('Elvis', 'Chidera');
echo "\n";
echo mixString('Elvis', '');
?>

http://ideone.com/MzyBlV
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Kidstell(op):
Elvisten10: ^^^, i guess i did not read the question well and missed the i need your algo part. Here is my code for this:
<?php
function mixString($str1, $str2) {
$str = $str1.$str2;
$i = strlen($str);
$temp = '';
while($i > 1) {
$i = $i - 1;
$j = mt_rand(0, $i);
$temp = $str[$j];
$str[$j] = $str[$i];
$str[$i] = $temp;
}
return $str;
}
echo mixString('Elvis', 'Chidera');
echo "\n";
echo mixString('Elvis', '');
?>

http://ideone.com/MzyBlV
the arrangement is definite and specific not random but i can notice your legend grade coding. Kudos***
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Elvisten10(m): 6:37am On Sep 25, 2013
Kidstell: the arrangement is definite and specific not random but i can notice your legend grade coding. My solution is not the best but i'm still reserving it
reserving it for what ?
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Kidstell(op): 1:33am On Sep 27, 2013
// $a and $b already known
$a='temple';$b='agile';
$al=strlen($a); $bl=strlen($b);
$diff=$bl-$al; $offset=abs($diff);
if($diff>0){
$twil=$b; $lite=$a;
}else{
$twil=$a;$lite=$b;
}
$tl=strlen($twil); $l=strlen($lite));
($tl>$l)?$part1=substr($twil,0,$offset-1):$part1='';
($tl>$l)?$mix1=substr($twil,$offset-1,$tl-1):$mix1=$twil;
$mix1=explode($mix1,''); $mix2=explode($lite,''); $mix='';
for($i=0;$i<$l;$i++){
$mix.=$mix1[$i].$mix2[$i];
}
$answer=$part1.$mix;
echo "string1= $a"; echo "string2= $b";
echo $answer;
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Elvisten10(m): 12:39pm On Sep 28, 2013
^^^ is that what you were reserving ?
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Javanian: 12:50pm On Sep 28, 2013
whats with PHP and $$$$$$$$$$$$$ it makes the code look ugly angry angry
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Elvisten10(m): 12:58pm On Sep 28, 2013
@kidstell, i ran your code and here is what i got www.ideone.com/pU4zZO
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Elvisten10(m): 1:04pm On Sep 28, 2013
Javanian: whats with PHP and $$$$$$$$$$$$$ it makes the code look ugly angry angry
i think the creators were trying to avoid the "DO NOT USE THIS" as a variable name.
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Elvisten10(m): 1:10pm On Sep 28, 2013
@kidstell, some errors i fixed in that code:
1)explode, you should have used str_split
2)double )) in your code.

Here is the edited copy www.ideone.com/KmIa6G
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Kidstell(op):
Elvisten10: @kidstell, some errors i fixed in that code:
1)explode, you should have used str_split
2)double )) in your code.

Here is the edited copy www.ideone.com/KmIa6G
when i said i noticed your legend grade, it's nice no one argued. I actually thought it should work if i just replaced str_split with explode. But there you are, if it were a puzzle, you just rank hi. Great ELVIS... One subtle error i think someone still needs to figure out is the last xter i.e "a" which is missing it works perfect when a and b have same lenght
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Kidstell(op): 10:11am On Oct 09, 2013
Had to reset offset. Kudos to the gurus on the post
http://ideone.com/bjlzIK
<?php
// $a and $b already known
$a='temple';$b='agile';
$al=strlen($a); $bl=strlen($b);
$diff=$bl-$al; $offset=abs($diff);
if($diff>=0){
$twil=$b; $lite=$a;
}else{
$twil=$a;$lite=$b;
}
$tl=strlen($twil); $l=strlen($lite);
($tl>$l)?$part1=substr($twil,0,$offset):$part1='';
($tl>$l)?$mix1=substr($twil,$offset,$tl):$mix1=$twil;
echo "mix1=$mix1**mix2=$lite**";
$mix1=str_split($mix1); $mix2=str_split($lite);
$mix='';
for($i=0;$i<$l;$i++){
$mix.=$mix1[$i].$mix2[$i];
}
$answer=$part1.$mix;
echo "**string1= $a**string2= $b";
echo $answer;
?>
http://ideone.com/dVQ9HV
Re: [MOD] PHP WORKOUTS - String Manipulation 1 by Nmeri17: 12:41am On Mar 03, 2016
Javanian:
whats with PHP and $$$$$$$$$$$$$ it makes the code look ugly angry angry
tongue tongue tongue

Re: [MOD] PHP WORKOUTS - String Manipulation 1 by AnthonyAk(m): 1:42am On Mar 03, 2016
pointless imo i mean how does one get a GET and POST request at the same time
1 Reply

Programming Challenge: Convert String to Json with a LoopPHP Workouts-sqlite3 CLASSES (wrapping Or Extending)How Do I Write Sql Statemen That Can Cast String To Double234

I Want To Be Developing .jar Aps For Nokia Phones. Where Do I Start?developing .jar Mobile Apps WhatHow To Deobfuscate .net Exe File | .net DeobfuscateSurvey - People Interested In Artificial Intelligence And Machine Learning