Tuesday, 28 May 2013

How to reverse string without php function

In many interview, interviewer asked me to write program for string reverse without php function, in this post i am shareing this program.

It's logic is very simple, just get substing from right han side and length should be one. 

<?php
            $stringToReverse = 'crunchcodes';
            $length = strlen($stringToReverse);

            for($i = $length; $i >= 0; $i--){
                  echo substr($stringToReverse, $i, 1);
            }
?>

Output is:

Actual string - crunchcodes
Outut string - sedochcnurc

No comments:

Post a Comment