J
Joe Wright
Irrwahn said:Dread, as I already said in another post: Sunday silliness.... :-/
Sorry, no, you invoke undefined behaviour by *computing* the invalid
value (Cf. ISO/IEC 9899:1999 6.5.6p8).
It's the only one of the three version, that does not invoke undefined
behaviour when fed with an empty string.
I started some of this, sorry. How about this last one?
char *revstr(char *s) {
char t, *b = s, *e;
if (s && *s) {
e = s + strlen(s);
while (b < e)
if (e - b > 1)
t = *b, *b++ = *--e, *e = t;
else
--e;
}
return s;
}
It is a NOP in the case of s == NULL or *s == '\0'. Also, unlike its
younger bretheren, it avoids reversing a one-character string or the
center character of an odd-length string.