C (gcc), 119 114 112... 111 111110 bytes
Outputs by modifying the input string.
p;f(char*s){for(char*r,*q=s;for(;*s;p*q=s;*q;p?*q++=*s++*s++=*q++:(s=rq=r))for(r=sr=q,p=0;*r?p+=(*r%17?strlen(sq):1)*~-(*r++%5&2):0;);*q=0;;*s=0;}
###How?
The algorithm is the same used in my JS answer with a few differences:
We use a
forloop instead of a recursive approach.We overwrite the input string with the output. This is safe because what is written is at most as long as what is read, and the meaningful information is always ahead of both the read and the write pointers (
sqandqsrespectively).Given the ASCII code
cof the move character, we usec % 17to find out if it's a vertical or horizontal move, andc % 5 & 2to distinguish between down and up or between left and right.| 'D' (68) | 'L' (76) | 'R' (82) | 'U' (85) ---------+----------+----------+----------+---------- % 17 | 0 | 8 | 14 | 0 % 5 & 2 | 2 | 0 | 2 | 0