Reverse a string in place

P

pete

RoS said:
In data Fri, 14 Dec 2007 20:53:42 +0100, RoS scrisse:


yes i have seen it ...

void reverse(char *aa)
{char *a=aa, *b, t;
if(a==0||*a==0) return;
for( ; *a; ++a);
for(--a, b=aa; b<a; ++b, --a)
{t=*b; *b=*a; *a=t;}
}

this is my version whithout compile it nor debug it

OK
but str* were not implementation reserved?

No.
The rules are more complicated than that.

N869
7.26.10 General utilities <stdlib.h>
[#1] Function names that begin with str and a lowercase
letter (possibly followed by any combination of digits,
letters, and underscore) may be added to the declarations in
the <stdlib.h> header.

7.26.11 String handling <string.h>
[#1] Function names that begin with str, mem, or wcs and a
lowercase letter (possibly followed by any combination of
digits, letters, and underscore) may be added to the
when this case "*end!=0" is verified here? (never)

You are correct!
 
C

CBFalconer

pete said:
You can't read code anymore. I know that you haven't tested the
code either. I've tested the code.

Evidently poorly. Just look at the snippet. The for line exits
when *end == 0. If *string is true, then the string does NOT start
with a 0, and end has been advanced from string, and the code
continues into the unshown area. However, if *string is 0 the else
clause arises, and the function returns without doing anything.

Of course it can be better written, but that does not affect the
action.

IIRC this was used in a string reversal function. Note that
reversing an empty string should be the same empty string, and
reversing a single char string should be the same single char
string.
 
O

ozbear

Evidently poorly. Just look at the snippet. The for line exits
when *end == 0. If *string is true, then the string does NOT start
with a 0, and end has been advanced from string, and the code
continues into the unshown area. However, if *string is 0 the else
clause arises, and the function returns without doing anything.

Of course it can be better written, but that does not affect the
action.

IIRC this was used in a string reversal function. Note that
reversing an empty string should be the same empty string, and
reversing a single char string should be the same single char
string.

Chuck/Pete,
At this point, I am unsure who is agreeing/disagreeing with whom.

My claim was that Lew's original correction to his original code
was flawed. In order to take care of the case when the string
supplied was "", Lew added the

if (*end) --end; else return;

line, which, of course can never be true and will always return
since in order to reach this test the /for/ loop terminates with
*end being false. While this works just Jim Dandy for empty
strings (for loop terminates immediately, and /if/ statement
then causes a /return/) it also returns for *any* string without
reversing anything.

My correction to text for this corner case is to change *end with
*string, which is the unmodified pointer to the beginning of
the string.

Pete agreed (I think) with my correction.
Chuck disagreed (I think) with either my correction or Pete.

Oz
 
C

CBFalconer

ozbear said:
At this point, I am unsure who is agreeing/disagreeing with whom.

My claim was that Lew's original correction to his original code
was flawed. In order to take care of the case when the string
supplied was "", Lew added the

if (*end) --end; else return;

line, which, of course can never be true and will always return
since in order to reach this test the /for/ loop terminates with
*end being false. While this works just Jim Dandy for empty
strings (for loop terminates immediately, and /if/ statement
then causes a /return/) it also returns for *any* string without
reversing anything.

I think you are all missing what happens when the routine is passed
an empty string to reverse, i.e.:

char s[SZ];
....
/* something fills s */
strcpy(s, ""); /* is the critical value */
reverse(s);

and consider what happens when the value of s is "", i.e. empty.
When the string has only one char things are also special, but the
usual handling covers it.

If the string is non-empty the test (*string) is true, --end is
executed, and end no longer points to a zero, but to the last
character in the string.
 
O

ozbear

ozbear said:
At this point, I am unsure who is agreeing/disagreeing with whom.

My claim was that Lew's original correction to his original code
was flawed. In order to take care of the case when the string
supplied was "", Lew added the

if (*end) --end; else return;

line, which, of course can never be true and will always return
since in order to reach this test the /for/ loop terminates with
*end being false. While this works just Jim Dandy for empty
strings (for loop terminates immediately, and /if/ statement
then causes a /return/) it also returns for *any* string without
reversing anything.

I think you are all missing what happens when the routine is passed
an empty string to reverse, i.e.:

char s[SZ];
....
/* something fills s */
strcpy(s, ""); /* is the critical value */
reverse(s);

and consider what happens when the value of s is "", i.e. empty.
When the string has only one char things are also special, but the
usual handling covers it.

If the string is non-empty the test (*string) is true, --end is
executed, and end no longer points to a zero, but to the last
character in the string.

I didn't miss that point, but it isn't what Lew originally wrote,
since he was testing *end, not *string, for the early return
in the case of an empty string (please look at his original
"correction" to cater for empty strings). I corrected it.

Do you dispute that:
1) The original addition of testing *end was incorrect.
2) My modification corrects that flaw.
3) Lew's original correction for empty strings will cause the
code to not reverse -any- string (just not have UB for
empty ones).

If you disagree with any of (1), (2) or (3), please tell me why.
If you don't, then I don't understand what you were objecting to
in the first place.

Just askin'.

Oz
 
P

pete

CBFalconer wrote:
However, if *string is 0 the else
clause arises, and the function returns without doing anything.

That's what it's supposed to do.

No action is required to reverse a zero length string.

If (*string) is 0,
then you are reversing a zero length string.

void reverse(char *string)
{
char *end;

for (end = string ; *end ; ++end);
if (*string) --end; else return;
 
P

pete

ozbear wrote:
My claim was that Lew's original correction to his original code
was flawed. In order to take care of the case when the string
supplied was "", Lew added the

if (*end) --end; else return;

line, which, of course can never be true and will always return
since in order to reach this test the /for/ loop terminates with
*end being false. While this works just Jim Dandy for empty
strings (for loop terminates immediately, and /if/ statement
then causes a /return/) it also returns for *any* string without
reversing anything.

My correction to text for this corner case is to change *end with
*string, which is the unmodified pointer to the beginning of
the string.

Pete agreed (I think) with my correction.

You are correct.
 
C

CBFalconer

ozbear said:
.... snip ...

I didn't miss that point, but it isn't what Lew originally wrote,
since he was testing *end, not *string, for the early return
in the case of an empty string (please look at his original
"correction" to cater for empty strings). I corrected it.

Do you dispute that:
1) The original addition of testing *end was incorrect.
2) My modification corrects that flaw.
3) Lew's original correction for empty strings will cause the
code to not reverse -any- string (just not have UB for
empty ones).

If you disagree with any of (1), (2) or (3), please tell me why.
If you don't, then I don't understand what you were objecting to
in the first place.

Here is the original quote. string is the input parameter, and end
is local to the function.

The above code exits for an empty string input. Only. I was
objecting to the misinterpretation of the code by others.
 
B

Ben Bacarisse

CBFalconer said:
Here is the original quote. string is the input parameter, and end
is local to the function.


The above code exits for an empty string input. Only. I was
objecting to the misinterpretation of the code by others.

Who misinterpreted it? You certainly seemed to, since when the
correction (from *end to *string) was suggested by ozbear, your reply
started with the word "No". It was:

| ozbear wrote:
| > Shouldn't that be:
| >
| > if (*string) --end; else return;
| > ^^^^^^
|
| No, because that clause detects the original supply of string as
| "", when end was never advanced, and exits early.

What you say after "No" suggests you know what it does, but I just
can't see why you start "No, ...". It made me think you had missed
the point of the correction. If you had replied "Yes, that clause..."
then I think this whole sub-thread would never have happened.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top