www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/09/28/02:49:13

Message-ID: <3BB4052E.954A4F69@yahoo.com>
From: CBFalconer <cbfalconer AT yahoo DOT com>
Organization: Ched Research
X-Mailer: Mozilla 4.75 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Benchmark Testing
References: <b4o2rtg4vhngk1ielgftmdo2jk4d9ogu86 AT 4ax DOT com> <9osdbt$r8k$1 AT nets3 DOT rz DOT RWTH-Aachen DOT DE> <ao54rtg7v4un2mc04466ch55eq53fvha1h AT 4ax DOT com> <9ov09c$gig$1 AT nets3 DOT rz DOT RWTH-Aachen DOT DE> <ep86rtg0mp41toqlis77ojcd50mojb3tc7 AT 4ax DOT com> <4634-Thu27Sep2001154412+0300-eliz AT is DOT elta DOT co DOT il> <nb27rt0c7nnaok3t4i5j8ojenegg0085ba AT 4ax DOT com>
Lines: 90
Date: Fri, 28 Sep 2001 06:39:02 GMT
NNTP-Posting-Host: 12.90.167.159
X-Complaints-To: abuse AT worldnet DOT att DOT net
X-Trace: bgtnsc06-news.ops.worldnet.att.net 1001659142 12.90.167.159 (Fri, 28 Sep 2001 06:39:02 GMT)
NNTP-Posting-Date: Fri, 28 Sep 2001 06:39:02 GMT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019