arrays - Reverse string using for loop only in php -


if there 1 array:

$arr = array('o', 'v', 'e', 'r','f', 'l', 'o', 'w'); 

and want display output 1st string = revo , 2nd string = flow using for loop only in php. not use inbuilt php function.

so how can it? please me.

the solution is:

$arr = array('o', 'v', 'e', 'r','f', 'l', 'o', 'w');             |_________________| |_________________|                      ^                    ^              reverse half  keep half 

then loop through array create first , second string.

$arr = array('o', 'v', 'e', 'r','f', 'l', 'o', 'w');  // reverse first half of array $arrlength = count($arr); for($i = 0; $i < $arrlength / 4; ++$i){     $tmp = $arr[$i];     $arr[$i] = $arr[(($arrlength / 2) - 1) - $i];     $arr[(($arrlength / 2)  - 1) - $i] = $tmp; }  // loop through array create first , second string $firststring = $secondstring = ""; for($i = 0; $i < $arrlength; ++$i){     if($i < $arrlength / 2){         $firststring .= $arr[$i];     }else{         $secondstring .= $arr[$i];       } }  echo "first string: " . $firststring . "<br />"; echo "second string: " . $secondstring . "<br />"; 

output:

first string: revo second string: flow 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -