Re: String reversing problem



Chuck F. wrote:
> tmp123 wrote:
> > int revstring ( char *s )
> > {
> > char *e;
> > int r;
> > for( e=s+(r=strlen(s))-1; s<e; *s^=*e^=*s^=*e, s++, e--);
> > return r;
> > }
>
> FYI your version invokes undefined behaviour. Try it with a string
> of zero chars. My length test is not there for fun. I also have
> evil suspicions about the xor operations.
>

Hi,

If length is 0, then e=s-1, thus s<e is false exiting loop.

However, if the usage of pointer comparation and pointer substraction
is not welcome, another version:

void revstring ( char *s )
{
char *e;
for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=*e);
}

Kind regards.

PS: some days ago, someone posted about if "C is easy". I do not known,
but it is tricky.

.