Reverse string without strrev

前端 未结 23 960
忘了有多久
忘了有多久 2020-12-13 13:19

Some time ago during a job interview I got the task to reverse a string in PHP without using strrev.

My first solution was something like this:

相关标签:
23条回答
  • 2020-12-13 13:56
    $string = 'abc';
    
    $reverted = implode(array_reverse(str_split($string)));
    
    0 讨论(0)
  • 2020-12-13 13:57

    Try This

    <?php
    $str="abcde";
    for($i=strlen($str)-1;$i>=0;$i--){
        echo $str[$i];
    }
    ?>
    

    output

    edcba

    0 讨论(0)
  • 2020-12-13 13:57

    Reverse string using recursion function.

    $reverseString = '';
    function Reverse($str, $len)
    {
        if ($len == 0) {
            return $GLOBALS['reverseString'];
        } else {
            $len--;
            $GLOBALS['reverseString'] .= $str[$len];
    
            return Reverse($str, $len);
        }
    }
    
    $str = 'Demo text';
    $len = strlen($str);
    echo Reverse($str, $len)
    
    0 讨论(0)
  • 2020-12-13 14:02

    Basically @EricBouwers answer, but you can remove the 2nd placeholder variable $j

    function strrev2($str)
    {
        $len = strlen($str);
        for($i=0;$i<$len/2;$i++)
        {
            $tmp = $str[$i];
            $str[$i] = $str[$len-$i-1];
            $str[$len-$i-1] = $tmp;
        }
    
        return $str;
    }
    

    Test for the output:

    echo strrev2("Hi there!"); // "!ereht iH"
    echo PHP_EOL;
    echo strrev2("Hello World!"); // "!dlroW olleH"
    

    This will go through the list and stop halfway, it swaps the leftmost and rightmost, and works it's way inward, and stops at the middle. If odd numbered, the pivot digit is never swapped with itself, and if even, it swaps the middle two and stops. The only extra memory used is $len for convenience and $tmp for swapping.

    If you want a function that doesn't return a new copy of the string, but just edits the old one in place you can use the following:

    function strrev3(&$str)
    {
        $len = strlen($str);
        for($i=0;$i<$len/2;$i++)
        {
            $tmp = $str[$i];
            $str[$i] = $str[$len-$i-1];
            $str[$len-$i-1] = $tmp;
        }
    }
    
    $x = "Test String";
    echo $x;           // "Test String"
    strrev3($x);
    echo PHP_EOL;
    echo $x;           // "gnirtS tseT"
    

    Using &$str passes a direct pointer the the string for editing in place.

    And for a simpler implementation like @treegardens, you can rewrite as:

    $s = 'abcdefghijklm';
    $len = strlen($s);
    for($i=0; $i < $len/2; $i++) {
        list($s[$i], $s[$len-$i-1]) = array($s[$len-$i-1], $s[$i]);
    }
    echo $s;
    

    It has the similar logic, but I simplified the for-loop quite a bit.

    0 讨论(0)
  • 2020-12-13 14:02

    Try this

    $warn = 'this is a test'; 
    $i=0;
    while(@$warn[$i]){
      $i++;} 
    while($i>0) 
    { 
      echo $warn[$i-1]; $i--;
    }
    
    0 讨论(0)
  • 2020-12-13 14:05

    You could use the XOR swap trick.

    function rev($str) {
        $len = strlen($str);
    
        for($i = 0; $i < floor($len / 2); ++$i) {
            $str[$i] = $str[$i] ^ $str[$len - $i - 1];
            $str[$len - $i - 1] = $str[$i] ^ $str[$len - $i - 1];
            $str[$i] = $str[$i] ^ $str[$len - $i - 1];
        }
    
        return $str;
    }
    
    print rev("example");
    
    0 讨论(0)
提交回复
热议问题