Junaid Junaid - 1 year ago 45
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

Answer Source

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

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

echo $temp.$out;