Mail Archives: djgpp/2000/05/24/10:30:39
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 -