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:
$s = 'abcdefg'; $temp = ''; for ($i = 0, $length = mb_strlen($s); $i < $length; $i++) { $temp .= $s{$length - $i - 1}; } var_dump($temp); // outputs string(7) "gfedcba"
then they asked me if I could do this without doubling the memory usage (not using the $temp
variable or any variable to copy the reversed string to) and I failed. This kept bugging me and since then I tried to solve this multiple times but I constantly failed.
My latest try looks like this:
$s = 'abcdefg'; for ($i = 0, $length = mb_strlen($s); $i < $length; $i++) { $s = $s{$i * 2} . $s; } var_dump($s); // outputs string(14) "gfedcbaabcdefg"
It's not a solution to chop off "abcdefg" after the loop because then I would still double the amount of memory used. I need to remove the last character in every iteration of the loop.
I tried to use mb_substr
like this:
$s = 'abcdefg'; for ($i = 0, $length = mb_strlen($s); $i < $length; $i++) { $s = $s{$i * 2} . mb_substr($s, $length - $i - 1, 1); } var_dump($s);
but it only gives me Uninitialized string offset
errors.
This is where I'm stuck (again). I tried googling but all the solutions I found either echo
the characters directly or use a temporary variable.
I also found the Question PHP String reversal without using extra memory but there's no answer that fits my needs.
A given string can be reversed in the C language by using strrev function,without strrev, recursion, pointers, using another string, or displaying it in reverse order.
That's an interesting one. Here's something I just came up with:
$s = 'abcdefghijklm'; for($i=strlen($s)-1, $j=0; $j<$i; $i--, $j++) { list($s[$j], $s[$i]) = array($s[$i], $s[$j]); } echo $s;
list()
can be used to assign a list of variables in one operation. So what I am doing is simply swapping characters (starting with first and last, then second-first and second-last and so on, till it reaches the middle of the string)
Output is mlkjihgfedcba
. Not using any other variables than $s
and the counters, so I hope that fits your criteria.
You can use the fact that in PHP a string can be thought of as an array of characters.
Then basically what you want to do is to replace each character $i
on the left side of the middle of the string with the character $j
on the right side of the middle with the same distance.
For example, in a string of seven characters the middle character is on position 3. The character on position 0 (distance 3) needs to be swapped with the character on position 6 (3 + 3), the character on position 1 (distance 2) needs to be swapped with the character on position 5 (3 + 2), etc.
This algorithm can be implemented as follows:
$s = 'abcdefg'; $length = strlen($s); for ($i = 0, $j = $length-1; $i < ($length / 2); $i++, $j--) { $t = $s[$i]; $s[$i] = $s[$j]; $s[$j] = $t; } var_dump($s);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With