Breaking

Sunday, March 8, 2020

How to write a function to reverse a given string without using string reversal functions in PHP?

To reverse a string without using PHPs inbuilt string function, we have different options. So the first option is instead of using inbuilt function we can use different required functions like strlen().

So, why strlen()?

Because to reverse a given string without using string reversal functions in PHP, first thing that comes into mind is that how many characters are there in the string?

PHP has provided solution over this to count a number of characters present in the string and the solution is using strlen().

Let's jump right into the first of the many solutions over the question.

$sting = "PHP is the beautiful language";
$stringLength = strlen($sting);
$reverse = '';
for ($i = $stringLength; $i >= 0; $i--)
{
    $reverse .= @$str[$i];
}
echo $reverse;

// output

egaugnal lufituaeb eht si PHP

Here,
In $str_len variable, we've stored the number of characters present in the string.
Then, in the for loop, we assigned $i = $str_len because of which we will get the last character of the string and that character we'll store in the $final_str variable and so on.


Other solution
This solution does not use any inbuilt function and it will provide us a reverted string.

$string = 'reverse';
$reverse = '';
$idx = 0;
while(@$string[$idx] != ''){
      $reverse = $string[$idx].$reverse;
      $idx++;
}
echo $reverse;

// output

egaugnal lufituaeb eht si PHP

Here,
we are not using any inbuilt function. we have defined string in the variable $string and to collect reverted string we have defined a variable $reverse.
Now, we have initialized $idx to 0 that will be used in the while loop. In the loop, we just make sure that $string[$idx] should not be equal to blank and to avoid any notice we have added @ sign (which we can disable in php.ini for production).
In the loop, we've assigned first character of the string along with concatination of newly created ($reverse) variables value to the $reverse variable.

So in the loop, it will reverse the variable like this-

First loop-

$reverse = '';
// inside while loop
$reverse = 'r'.'';

Now outer $reverse variable becomes
$reverse = 'r';

Second loop-

$reverse = 'r';
// inside while loop
$reverse = 'e'.'r';

Now outer $reverse variable becomes-
$reverse = 'er';

Third loop-

$reverse = 'er';
// inside while loop
$reverse = 'v'.'er';

Now outer $reverse variable becomes-
$reverse = 'ver';

Fourth loop-

$reverse = 'ver';
// inside while loop
$reverse = 'e'.'ver';

Now outer $reverse variable becomes-
$reverse = 'ever';

Fifth loop-

$reverse = 'ever';
// inside while loop
$reverse = 'r'.'ever';

Now outer $reverse variable becomes-
$reverse = 'rever';

Sixth loop-

$reverse = 'rever';
// inside while loop
$reverse = 's'.'rever';

Now outer $reverse variable becomes-
$reverse = 'srever';

Seventh loop-

$reverse = 'srever';
// inside while loop
$reverse = 'e'.'srever';

Now outer $reverse variable becomes-
$reverse = 'esrever';

So,
$reverse variable now contains reverted string as 'esrever'.
Let me know if you have any questions on any of the above solution and if you guys have any other excellent solution then please comment.



No comments:

Post a Comment