Last active
September 12, 2019 16:44
-
-
Save Xiradorn/a158eabb503ba9491739f85c83044d76 to your computer and use it in GitHub Desktop.
simple array algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple inversion algo | |
*/ | |
/** | |
* Function for string inversion. | |
* @param string $str just a normal text string | |
* @return string $str same string but inverted | |
*/ | |
function xir_ary_inv( string $str ) | |
{ | |
$str = str_split( $str ); | |
$max = count( $str ); | |
$middle = ( $max % 2 ) ? ( $max - 1 ) / 2 : $max / 2; | |
for ( $i = 0; $i < $middle; $i++ ) | |
{ | |
if ( ( $max % 2 ) == 0 and $i == ( $max / 2 ) ) continue; | |
$n = $max - 1; | |
$t = $str[ $i ]; | |
$str[ $i ] = $str[ $n - $i ]; | |
$str[ $n - $i ] = $t; | |
} | |
$str = implode( '', $str ); | |
return $str; | |
} | |
echo xir_ary_inv( "Tony" ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment