Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,907 members, 7,802,952 topics. Date: Saturday, 20 April 2024 at 04:56 AM

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

Nairaland Forum / Science/Technology / Programming / [MOD] PHP WORKOUTS - String Manipulation 1 (6052 Views)

Programming Challenge: Convert String to Json with a Loop / PHP Workouts-sqlite3 CLASSES (wrapping Or Extending) / How Do I Write Sql Statemen That Can Cast String To Double (2) (3) (4)

(1) (Reply) (Go Down)

[MOD] PHP WORKOUTS - String Manipulation 1 by Kidstell: 10:41am On Sep 20, 2013
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): 4:06pm On Sep 23, 2013

<?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;

?>


1 Like

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: 12:35am On Sep 25, 2013
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: 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: 12:53am On Oct 09, 2013
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: 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)

Download Asp.net Mvc 5 Essential Training Video Tutorial / Shutting Down A Computer Using Notepad (beginners Tutorial) / Watch Google Do A Barrel Roll!

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.