Mail Archives: djgpp/2001/09/28/02:49:13
Radical, NetSurfer wrote: (and topposted, fixed)
>
> 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.
>
> /*
> ** 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) {
void str...
> char *last, temp;
>
> last = string + strlen(string); /* points to '\0' */
> while (last-- > string) {
> temp = *string; *string++ = *last; *last = temp;
> }
> return (char*)string; /* NO SUCH LINE */
>
> } /* strrev */
Kindly do not alter the code I published to make it incorrect
without claiming the credit for the alterations. My function
returned void.
Also, kindly do NOT toppost.
>
> strrev(), according to every compiler I've seen that supports it,
> always returns a pointer to the modified string.
If such a return is really desirable (which I question), my code
can be modified as follows:
/* ======================= */
/* reverse string in place */
char *strrev(char *string) {
char *last, *str, temp;
last = string + strlen(string); /* points to '\0' */
str = string;
while (last-- > str) {
temp = *str; *str++ = *last; *last = temp;
}
return string;
} /* strrev */
What you did not point out is that Bob Stouts code guards against
a NULL input pointer, and mine does not. Neither do most library
implementations of string routines. They are expected to operate
on strings, not NULL pointers. Guarding the while with "if (str)"
adds that feature, if desired.
A point for the language mavens: If char is a signed type, then
are the xor operators necessarily valid in Bobs code? In
practice, I would expect them to work.
--
Chuck F (cbfalconer AT yahoo DOT com) (cbfalconer AT XXXXworldnet DOT att DOT net)
(Remove "XXXX" from reply address. yahoo works unmodified)
mailto:uce AT ftc DOT gov (for spambots to harvest)
- Raw text -