Junaid Junaid - 3 months ago 10x
PHP Question

Reverse order of words in a sentence without using built in functions

I had an interview and a question that seems really simple was not clicking in my mind. Just want to get the answer for it because have tried a lot and unable to find solution.

I have to write a sentence in opposite like

input => My Name Is Junaid
output => Junaid Is Name My

Should not use ANY built-in function of PHP



I did upto this

$string = "My Name Is Junaid";
$len = strlen($string);
for($i=$len; $i > 0; $i--){
echo $string[$i-1];

it results

dianuJ sI emaN yM

need some more tweaking


$str = "My Name is Fred";
$i = 0;
while( $d = $str[$i] )
    if( $d == " "){

        $out = " ".$temp.$out;
        $temp = "";

echo $temp.$out;