r/ProgrammerHumor • • Feb 05 '18

StackOverflow in a nutshell.

Post image
16.2k Upvotes

1.2k comments sorted by

View all comments

Show parent comments

5

u/PTRWP Feb 06 '18

Pretty off topic here, but why did you do the revered string like that? I'm still learning, but I've reversed strings by creating a new string then using for (i=string.length(); i>0; i--) and within the loop adding the current character to the end of the new string. I made it into a method so I haven't bothered to redo it in a different way since.

4

u/ar-pharazon Feb 06 '18

it avoids allocating a new string (though it is destructive, meaning that if you needed your original, unreversed string, you're going to have to reverse it again).

your method has the benefit of not destroying the original, but the particular way you wrote it is prone to triggering extra allocations. if you're using java, consider the following:

String forwards = "forwards";
String rev = "";
for (int i = forwards.length() - 1; i >= 0; i--) rev = rev + forwards[i];

in every iteration of the loop, we're actually discarding the old value of rev and replacing it with a copy that has the new character appended. this means we're generating a lot of unnecessary garbage.

if, on the other hand, you're in c++ using something like string::append, you're probably going to end up doing a vector resize (or similar operation) at some point.

but the bottom line is that extra allocation is unnecessary because you know beforehand exactly how much memory you need: forwards.length() characters. the java code to do this with only one allocation is roughly:

String forwards = "forwards";
char[] rev = new char[forwards.length()];
for (int i = 0; i < forwards.length(); i++) rev[forwards.length() - i - 1] = forwards[i];
String result = new String(rev);

c++ would be similar, though you could use the fill constructor rather than having the intermediate char array.

1

u/PTRWP Feb 06 '18

Thanks for the explanation

1

u/Kinglink Feb 06 '18

So what are we trying to do is important?

Usually we're trying to reverse a string in place. In this case using a second string/buffer is completely unnecessary and wasteful, especially on huge strings. If on the other hand we're trying to make a second string that's the reverse of the first your method is acceptable. However make sure you reserve or allocate the correct size of the string before hand otherwise you'll be doing memory allocations all over the place.

Assuming you just want to reverse a string in place, you only need 1 extra byte (unless you want to get real fancy and use Xor)

You also only need to do it for half. So here's how I'd do it.

char*  pStr = blah blah blah
int32 nStringLength = string.length();
for (int32 i = 0; i < nStringLength/2; ++i)
{
    char temp = pStr[i];
    pStr[i] = pStr[nStringLength - i - 1];
    pStr[nStringLength - i - 1] = temp;
}

A couple notes. Notice that it'll only loop 2 times on a 5 character string, as the 3rd (middle) character is already in the right place. ++i is better than i++ because i++ stores the value of i, increments it and then returns the stored value of i, a minor optimization but in general try not to use i++ or i--.

Let's talk a crazier way. Xor Swapping, you can replace the inside of the function with this.

pStr[i] = pStr[i] ^ pStr[nStringLength - i - 1];
pStr[nStringLength - i - 1] = pStr[i] ^ pStr[nStringLength - i - 1];
pStr[i] = pStr[i] ^ pStr[nStringLength - i - 1];

This does the same swap but doesn't require an additional memory location.