Junaid Junaid - 4 months ago 14
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

Thanks

EDIT

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

Answer
<?php

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

        $out = " ".$temp.$out;
        $temp = "";
    }else{
        $temp.=$d;

    }
    $i++;
}
echo $temp.$out;
?>