473,795 Members | 2,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Highly efficient string reversal code

Hello, I'm a highly experienced expert C programmer and I've written
this code to reverse a string in place. I think you could all learn
something from it!

int reverse(char* reverseme){
int retval=-1;
if(retval!=NULL ){
int len=strlen(retv al){
if(len>0){
int half=len>>1;
for(;retval<hal f;retval++){
reverseme[retval]^=reverseme[len-(retval+1)];
reverseme[len-(retval+1)]=reverseme[retval];
reverseme[retval]^=reverseme[len-(retval+1)];
}
}
}
return retval;
}

-Phil/CERisE
Sep 22 '08 #1
144 5012
do************* *@ymail.com wrote:
Hello, I'm a highly experienced expert C programmer
No, you are not.
and I've written
this code to reverse a string in place. I think you could all learn
something from it!
I doubt it. You should rethink it. It is not, as you subject line
claims "highly efficient".
Sep 22 '08 #2
On September 22, 2008 14:42, in comp.lang.c, do************* *@ymail.com
(do************ **@ymail.com) wrote:
Hello, I'm a highly experienced expert C programmer
Apparently, you are
a) egotistical, and
b) wrong
and I've written
this code to reverse a string in place.
I am /so/ sorry, both for you and your employer.
I think you could all learn
something from it!
I'm sure that you are right. After all, /I/ learned that you are much less
than the C programmer that you claim to be.
int reverse(char* reverseme){
int retval=-1;
if(retval!=NULL )
Useless test, as retval was initialized (and never altered) to a value
that will not equate to NULL
{
int len=strlen(retv al)
First real failure. retval is an integer, and the argument to
strlen() is a pointer-to-char. You can't get a valid result from
taking the "string length" of an integer.

Second failure: you did not #include the header that declares the
strlen() function. Thus, your compiler did not flag the above
assignment as an error (argument of incompatable type)
{
if(len>0)
So, what is the "string length" of an integer? And when would it be
greater than zero?
{
int half=len>>1;
Style hack: To be clearer to the intent of what sort of number
half represents, you should have coded this as
int half = len / 2;
Of course, since len is a nonsense value, half will also be a
nonsense value.

for(;retval<hal f;retval++){
reverseme[retval]^=reverseme[len-(retval+1)];
Undefined behaviour on first iteration, when retval == -1
reverseme[-1] is out of bounds.
reverseme[len-(retval+1)]=reverseme[retval];
Undefined behaviour on first iteration - same reason

reverseme[retval]^=reverseme[len-(retval+1)];
Undefined behaviour on first iteration - same reason

Plus, you've screwed up the "xor trick", and wiped out /both/
ends of the string with invalid values.

The head end now contains an array of 0x00 (up to
the ill-computed "midpoint") , while the tail end contains the
results of the head end xor the tail end.
}
}
}
return retval;
Of what use is this return value?
}

-Phil/CERisE
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Sep 22 '08 #3
do************* *@ymail.com wrote:
Hello, I'm a highly experienced expert C programmer [...]
s/C programmer/PLONKed troll/

--
Er*********@sun .com
Sep 22 '08 #4
do************* *@ymail.com wrote:
Hello, I'm a highly experienced expert C programmer and I've written
this code to reverse a string in place. I think you could all learn
something from it!
You should learn some better trolling techniques. I suggest studying
under Bill Cunningham.


Brian
Sep 22 '08 #5
Lew Pitcher wrote, On 22/09/08 20:06:
On September 22, 2008 14:42, in comp.lang.c, do************* *@ymail.com
(do************ **@ymail.com) wrote:
>Hello, I'm a highly experienced expert C programmer

Apparently, you are
a) egotistical, and
b) wrong
Agreed. Although you missed some of the problems...
>and I've written
this code to reverse a string in place.

I am /so/ sorry, both for you and your employer.
You think s/he is employed?
>I think you could all learn
something from it!

I'm sure that you are right. After all, /I/ learned that you are much less
than the C programmer that you claim to be.
>int reverse(char* reverseme){
int retval=-1;
if(retval!=NULL )

Useless test, as retval was initialized (and never altered) to a value
that will not equate to NULL
<snip load more broken code>

You forgot to mention that as retval is an int it won't actually compile
on all implementations .

I think it was a troll. However, it was amusing for how bad the code
was. I wonder how many other errors it has that have been missed?
--
Flash Gordon
If spamming me sent it to sm**@spam.cause way.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Sep 22 '08 #6
Lew Pitcher <lp******@teksa vvy.comwrites:
On September 22, 2008 14:42, in comp.lang.c, do************* *@ymail.com
(do************ **@ymail.com) wrote:
[...]
> int retval=-1;
if(retval!=NULL )

Useless test, as retval was initialized (and never altered) to a value
that will not equate to NULL
[...]

It's not just useless. retval is an int; NULL expands to a null
pointer constant. If NULL happens to be #defined as 0, the test is
merely useless; if it's #defined as ((void*)0) or something similar,
the test is a constraint violation.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 22 '08 #7
Flash Gordon <sm**@spam.caus eway.comwrites:
[...]
I think it was a troll. However, it was amusing for how bad the code
was. I wonder how many other errors it has that have been missed?
I don't think anybody has mentioned the syntax error yet.
int len=strlen(retv al){
should be
int len=strlen(retv al);

I'd call it a comedy of errors, but that would require it to be funny.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 22 '08 #8
In article <dc************ **************@ TEKSAVVY.COM-Free>,
Lew Pitcher <lp******@teksa vvy.comwrote:
>On September 22, 2008 14:42, in comp.lang.c, do************* *@ymail.com
(do*********** ***@ymail.com) wrote:
>Hello, I'm a highly experienced expert C programmer

Apparently, you are
a) egotistical, and
b) wrong
>and I've written
this code to reverse a string in place.

I am /so/ sorry, both for you and your employer.
>I think you could all learn
something from it!

I'm sure that you are right. After all, /I/ learned that you are much less
than the C programmer that you claim to be.
Since the subject has come up, here's one I've been waiting for an
excuse to post:
--------
typedef struct l{char a;struct l*b;}l;
static void x(char*a,l*b) {if(b){(-1)[a]=b->a;x(--a,b->b);}}
static void y(char*a,l*b,l* c) {if(b){l*d=b->b;b->b=c;y(a,d,b);} else{x(a,c);}}
static void z(char*a,l*b) {if(*a){l c;c.a=*a;c.b=b; z(a+1,&c);}else {y(a,b,0);}}
void reverse(char *s) { z(s,0); }
--------
Unlike the one that started the thread, this has actually been tested
and verified to work.

It also uses advanced implementation techniques that allow programs
with certain properties (especially in languages with properties that
make those program properties easy to detect and exploit) to be
compiled to highly efficient code.

Somebody who spends enough time untangling it might even learn
something useful from it (for sufficiently, but not comically, loose
values of "useful").
dave
(bonus points if you can figure out where the idea to do it that way came from)

--
Dave Vandervies dj3vande at eskimo dot com

None of my copies of K&R have a "screen". Should I apply for a refund?
--Chris Dollin in comp.lang.c
Sep 22 '08 #9
On Mon, 22 Sep 2008 11:42:37 -0700, dominantubergee k wrote:
Hello, I'm a highly experienced expert C programmer and I've written
this code to reverse a string in place. I think you could all learn
something from it!
Boy, that last sure is going to endear you to people here!

Sep 22 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.