delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/24/09:45:29

From: buers AT gmx DOT de (Dieter Buerssner)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: WANTED: strrev() where is it???
Date: 24 May 2000 13:34:30 GMT
Lines: 43
Message-ID: <8ggt3h.3vs4d07.0@buerssner-17104.user.cis.dfn.de>
References: <8gfcsp$due$1 AT nnrp1 DOT deja DOT com> <392BC891 DOT B8A0D8 AT ma DOT tum DOT de>
NNTP-Posting-Host: pec-105-219.tnt5.s2.uunet.de (149.225.105.219)
Mime-Version: 1.0
X-Trace: fu-berlin.de 959175270 1382515 149.225.105.219 (16 [17104])
X-Posting-Agent: Hamster/1.3.13.0
User-Agent: Xnews/03.02.04
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Waldemar Schultz wrote:

>RadSurfer schrieb:
>> Do I have to resort to a routine to emulate  strrev()  ?

To be portable, yes. You should then also give it a different
name, because all names starting with str and followed by a
lower case letter, are reserved.

>#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;
>}

-- 
Regards, Dieter

- Raw text -


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