In many interview, interviewer asked me to write program for string reverse without php function, in this post i am shareing this program.
Output is:
Actual string - crunchcodes
Outut string - sedochcnurc
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);
}
?>
$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