Dale:
This actually has a bug:
int str_rev(char*s)
{
int length = strlen(s);
int pos = 0;
while(length > 0)
{
s[length--] = s[pos++];
}
}
On string abc\0 this will produce:
aaba
losing \0-termination.
I do not think you can manage to reverse without a true byte swap if
you want to do it in place. Also, you do not need to go through the
entire string, only half of it, otherwise you will end up with the
string being restored to the original.
The original be made faster by removing this check:
if (!s) return -1;
I also could have avoided string copy into the word buffer, and simply
output the characters of the string backwards - but the purpose of the
exercise was to demonstrate how fast str_rev() written in C would run,
not just to print a bunch of strings in reverse order.
--
Sasha Pachev
Fast Running Blog.
http://fastrunningblog.com
Run. Blog. Improve. Repeat.
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/