Mail Archives: djgpp/2001/09/27/16:34:14
/*
** STRREV.C - reverse a string in place
**
** public domain by Bob Stout
*/
char *strrev(char *str) {
char *p1, *p2;
if (! str || ! *str) return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
and how does the above code compare to, whats below,
in terms of CPU time, and memory-access, etc?
/* ======================= */
/* reverse string in place */
char *strrev(char *string) {
char *last, temp;
last = string + strlen(string); /* points to '\0' */
while (last-- > string) {
temp = *string; *string++ = *last; *last = temp;
}
return (char*)string;
} /* strrev */
strrev(), according to every compiler I've seen that supports it,
always returns a pointer to the modified string.
On Thu, 27 Sep 2001 15:44:13 +0200, "Eli Zaretskii"
<eliz AT is DOT elta DOT co DOT il> wrote:
>> From: Radical DOT NetSurfer AT delorie DOT com
>> Newsgroups: comp.os.msdos.djgpp
>> Date: Thu, 27 Sep 2001 09:06:49 -0400
>>
>> PLEASE libc.a people, __add__ strrev() its been missing for far too
>> long. Thanks.
>
>It's on my TODO.
- Raw text -