delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/24/10:30:39

From: Waldemar Schultz <schultz AT ma DOT tum DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: WANTED: strrev() where is it???
Date: Wed, 24 May 2000 15:59:38 +0200
Organization: [posted via] Leibniz-Rechenzentrum, Muenchen (Germany)
Lines: 62
Message-ID: <392BE04A.DCC6D707@ma.tum.de>
References: <8gfcsp$due$1 AT nnrp1 DOT deja DOT com> <392BC891 DOT B8A0D8 AT ma DOT tum DOT de> <8ggt3h DOT 3vs4d07 DOT 0 AT buerssner-17104 DOT user DOT cis DOT dfn DOT de>
NNTP-Posting-Host: pcritter10.mathematik.tu-muenchen.de
Mime-Version: 1.0
X-Trace: wsc10.lrz-muenchen.de 959176876 642 131.159.69.74 (24 May 2000 14:01:16 GMT)
X-Complaints-To: news AT lrz-muenchen DOT de
NNTP-Posting-Date: 24 May 2000 14:01:16 GMT
X-Mailer: Mozilla 4.6 [de] (Win98; I)
X-Accept-Language: de,en,en-US
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Dieter Buerssner schrieb:

> >#include <string.h>
> >
> >#if defined(__cplusplus) && __cplusplus
> > extern "C" {
> >#endif
> >char *strrev(char *str)
> >{
> >char *p1=NULL,*p2=NULL;
> 
> Minor point: initialization is not needed here.
> 
> >  if(! str || ! *str)
> >   return str;
> >  for(p1=str,p2=str+strlen(str)-1;p2>p1;*p1^=*p2,*p2^=*p1,
> >      *(p1++)^=*(p2--));
> 
> This is the reason for my followup. Why do you obfuscate the code,
> by using the xor trick for swapping, and by doing all the work
> in the for.
> 
> The xor-trick will produce slower code on x86 (and probably most
> architectures). It will need three XORs and three MOVs, compared
> to four MOVs by the straightforward swapping code. It will need
> one less register with current gcc, though.
> 
> Also, using straightforward swapping code has the advantage,
> that you don't have to think about problems with binary operations,
> when char is signed. But that of course is a language lawyer issue.
> 
> >  return str;

#include <string.h>

#if defined(__cplusplus) && __cplusplus
 extern "C" {
#endif
char *StrRev(char *str)
{
char p,*p1,*p2;

  if(! str || ! *str)
   return str;

  for(p1=str,p2=str+strlen(str)-1;p2>p1;p1++,p2--)
  {
   p  =*p1;
   *p1=*p2;
   *p2=p;
  }
  return str;
}
#if defined(__cplusplus) && __cplusplus
            }
#endif

hope you like that one.
-- 
 Gruss Waldemar Schultz.
 Technische Universität München, Zentrum Mathematik M1, D 80290 München
 Tel: +49 (0)89 2892 8226        FAX: +49 (0)89 2892 8228

- Raw text -


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