String Reverse Question

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.
 
I

Irrwahn Grausewitz

Joe Wright said:
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.

That looks OK. However, now that you've added the extra check
for *s != '\0', you can't invoke undefined behaviour anyway, and
thus can again simplify, without loss of efficiency, to:

char *revstr(char *s) {
char t, *b = s, *e;
if (s && *s) {
e = s + strlen(s);
while (b < --e)
t = *b, *b++ = *e, *e = t;
}
return s;
}

Funny, isn't it? :)

Regards
 
R

Richard Delorme

Irrwahn Grausewitz a écrit :
Sorry, no, you invoke undefined behaviour by *computing* the invalid
value (Cf. ISO/IEC 9899:1999 6.5.6p8).

Right, I am another victim of Sunday silliness ;-)
 
C

Christopher Benson-Manica

Old Wolf said:
p = malloc( (len + 1) * sizeof *p);
Of course, if sizeof *p == 1 as it does here, then these expressions
are the same, but the point of "sizeof *p" is to be able to avoid problems
related to what the type of "p" is.

Yep, I blew it, and was saved from disaster only by luck :|
 
C

Christopher Benson-Manica

James McIninch <[email protected]> spoke thus:

Please don't top-post. Thanks.
#include <stdlib.h>
#include <string.h>
char * strrev (const char *s)
{
char * p;
int i, j;
j = strlen(s)
if (p = calloc(1, j+1))

ITYM

if( p=calloc(j+1,1) );

The results, of course, are identical, but it matches the semantics of
calloc()'s prototype.
for (i=0; i<j; ++i)
p = s[j-i];


Per Barry's post, ITYM

p=s[j-i-1];

although of course changing the value of j appropriately before
entering the loop would be viable as well.
 
C

Christopher Benson-Manica

Rakesh said:
Just a bit curious here - Is it ok to allocate locally here, and
return it. Would that not lead to memory leaks.

Certainly not, as long as you're careful to free it where you returned
it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top