Reverse string without strrev

前端 未结 23 973
忘了有多久
忘了有多久 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 14:17

    This is my solution to solve this.

    $in = 'This is a test text';
    $out = '';
    // find string length
    $len = strlen($in);
    
    // loop through it and print  it reverse
    for ( $i = $len - 1; $i >=0;$i-- )
    {
        $out = $out.$in[$i];
     }
    
    echo $out;
    

提交回复
热议问题