By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,186 Members | 1,530 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,186 IT Pros & Developers. It's quick & easy.

To reverse a string

P: n/a
could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance

Mar 20 '06 #1
Share this Question
Share on Google+
47 Replies


P: n/a
"sudharsan" <su*********@gmail.com> wrote in news:1142873798.329339.167240
@e56g2000cwe.googlegroups.com:
could any one please give me a code to reverse a string of more than
1MB .???


I can think of several reasons why the length of the string might be
important, but why does it matter to you?

Is the strlen to find the end of the string bothering you?

Have you tried the trivial way of doing it in place for a string of any
size?

Sinan
--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

Mar 20 '06 #2

P: n/a
sudharsan opined:
could any one please give me a code to reverse a string of more than
1MB .???


Why do you think it would be different to the one for strings less than
1MB?

--
BR, Vladimir

I'll meet you... on the dark side of the moon...
-- Pink Floyd

Mar 20 '06 #3

P: n/a
sudharsan wrote:
could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance


Many C library implementations provide a non-standard strrev() function
which does what you want. If not, then you'll have to roll your own.
The size of the string shouldn't be a consideration if you're doing
this to learn programming or C. If you can implement the function is a
readable and straightforward manner and post your attempt here, the
regulars can help you further. It's quite easy to do.

Mar 20 '06 #4

P: n/a
On 2006-03-20, sudharsan <su*********@gmail.com> wrote:
could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance


wtrite some code to reverse a string that works for 0,1,2 and 3
characters and it will probably work for a megabyte of them too. size
is no indicator of complexity.
Mar 20 '06 #5

P: n/a
sudharsan wrote:
could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance


Here's one way of doing it.
NOTE: If your implementation already defines a strrev() function, then
rename the corresponding function in the following code to avoid linker
errors.

#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool strrev( char *s ) {
char tmp, *adv_p = NULL, *reg_p = NULL;
size_t l = 0;
ptrdiff_t seperation = 0;

if(s == NULL || (l = strlen(s)) < 2)
return false;
else {
adv_p = s;
--l;
reg_p = adv_p + l;
}

do {
tmp = *adv_p;
*adv_p = *reg_p;
*reg_p = tmp;
++adv_p;
--reg_p;
seperation = reg_p - adv_p;
} while(seperation > 0);

return true;
}

int main(void) {
char arr[32];

puts("Please enter your name:");
if(fgets(arr, 32, stdin) == NULL)
return EXIT_FAILURE;
else {
if(!strrev(arr))
puts("strrev() returned false.");
else
printf("Your name reversed:\n%s\n", arr);
}

return EXIT_SUCCESS;
}

Mar 20 '06 #6

P: n/a
"santosh" <sa*********@gmail.com> wrote in news:1142879921.107221.266900
@g10g2000cwb.googlegroups.com:
sudharsan wrote:
could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance
Here's one way of doing it.
NOTE: If your implementation already defines a strrev() function, then
rename the corresponding function in the following code to avoid
linker errors.

#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool strrev( char *s ) {
char tmp, *adv_p = NULL, *reg_p = NULL;
size_t l = 0;
ptrdiff_t seperation = 0;


separation
if(s == NULL || (l = strlen(s)) < 2)
return false;


Passing a NULL to this function is a violation of contract, it seems to
me. I would have been inclined to code that requirement into an assert.

On the other hand, passing an empty string or a string consisting of a
single character is benign. I would have just returned from this
function upon detecting that.

Are you using the return value as a 'success' indicator? In that case,
why is it an error condition to not modify the passed string?

Sinan

--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

Mar 20 '06 #7

P: n/a
A. Sinan Unur wrote:
"santosh" <sa*********@gmail.com> wrote in news:1142879921.107221.266900
@g10g2000cwb.googlegroups.com:
sudharsan wrote:
could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance
Here's one way of doing it.
NOTE: If your implementation already defines a strrev() function, then
rename the corresponding function in the following code to avoid
linker errors.

#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool strrev( char *s ) {
char tmp, *adv_p = NULL, *reg_p = NULL;
size_t l = 0;
ptrdiff_t seperation = 0;


separation
if(s == NULL || (l = strlen(s)) < 2)
return false;


Passing a NULL to this function is a violation of contract, it seems to
me. I would have been inclined to code that requirement into an assert.


Maybe your right. An assert() though would abort the program. What if
the caller may need to do other tasks, even if this function fails?
On the other hand, passing an empty string or a string consisting of a
single character is benign. I would have just returned from this
function upon detecting that.
Yes. You're right. I should probably just return true with 's'
unmodified. In that case I should seperate the tests for null pointer
and empty or single character string.
Are you using the return value as a 'success' indicator?
Yes.
In that case, why is it an error condition to not modify the passed string?


It shouldn't be. It was a decision on the spur of the moment. I'll
change it.
Thanks for your feedback.

Mar 20 '06 #8

P: n/a
On 2006-03-20, santosh <sa*********@gmail.com> wrote:
sudharsan wrote:
could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance
Here's one way of doing it.
NOTE: If your implementation already defines a strrev() function, then
rename the corresponding function in the following code to avoid linker
errors.

#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool strrev( char *s ) {
char tmp, *adv_p = NULL, *reg_p = NULL;
size_t l = 0;
ptrdiff_t seperation = 0;

if(s == NULL || (l = strlen(s)) < 2)
return false;


It's legal to reverse a string of one character. Or does "false" just
indicate no reversal was necessary?
else {
Its all an else in this case since you returned.

adv_p = s;
--l;
reg_p = adv_p + l;
}

do {
tmp = *adv_p;
*adv_p = *reg_p;
*reg_p = tmp;
++adv_p;
--reg_p;
seperation = reg_p - adv_p;
} while(seperation > 0);

return true;
}
nicer just to use the strlen/2 IMO.

void reverse(char * refS){
/* assume parent does parameter checks */
unsigned int len = (unsigned int)strlen(refS);
char * refE=refS+len-1; /* if len is 0 no problem since nothing done*/
len=len/2; /* or len>>=1 */
while(len--){
char c = *refS; /* front char */
*refS++ = *refE; /* is now end char */
*refE-- = c; /* end char now start char */
}
}


int main(void) {
char arr[32];

puts("Please enter your name:");
if(fgets(arr, 32, stdin) == NULL)
return EXIT_FAILURE;
else {
if(!strrev(arr))
puts("strrev() returned false.");
else
printf("Your name reversed:\n%s\n", arr);
}

return EXIT_SUCCESS;
}

--
Debuggers : you know it makes sense.
http://heather.cs.ucdavis.edu/~matlo...g.html#tth_sEc
Mar 20 '06 #9

P: n/a
"Richard G. Riley" <rg****@gmail.com> wrote in news:tub3f3-03i.ln1
@fujitsu.mydomain.com:
void reverse(char * refS){
/* assume parent does parameter checks */
unsigned int len = (unsigned int)strlen(refS);


size_t len = strlen(refS);

Sinan

--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
Mar 20 '06 #10

P: n/a
Richard G. Riley wrote:
On 2006-03-20, santosh <sa*********@gmail.com> wrote:
sudharsan wrote:
could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance


Here's one way of doing it.
NOTE: If your implementation already defines a strrev() function, then
rename the corresponding function in the following code to avoid linker
errors.

#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool strrev( char *s ) {
char tmp, *adv_p = NULL, *reg_p = NULL;
size_t l = 0;
ptrdiff_t seperation = 0;

if(s == NULL || (l = strlen(s)) < 2)
return false;


It's legal to reverse a string of one character. Or does "false" just
indicate no reversal was necessary?


Sinan pointed it out earlier. I should've decoupled the tests and
returned true for the case you suggest.
else {


Its all an else in this case since you returned.


I'm sorry, I don't get what you're trying to say here.
adv_p = s;
--l;
reg_p = adv_p + l;
}

do {
tmp = *adv_p;
*adv_p = *reg_p;
*reg_p = tmp;
++adv_p;
--reg_p;
seperation = reg_p - adv_p;
} while(seperation > 0);

return true;
}


nicer just to use the strlen/2 IMO.

void reverse(char * refS){
/* assume parent does parameter checks */
unsigned int len = (unsigned int)strlen(refS);
char * refE=refS+len-1; /* if len is 0 no problem since nothing done*/
len=len/2; /* or len>>=1 */
while(len--){
char c = *refS; /* front char */
*refS++ = *refE; /* is now end char */
*refE-- = c; /* end char now start char */
}
}


Your version is more concise. But why not use the proper type for
strlen()'s return value? What's the reason to cast it to an unsigned
int?

Thanks for the feedback.

Mar 20 '06 #11

P: n/a
On 2006-03-20, A. Sinan Unur <1u**@llenroc.ude.invalid> wrote:
"Richard G. Riley" <rg****@gmail.com> wrote in news:tub3f3-03i.ln1
@fujitsu.mydomain.com:
void reverse(char * refS){
/* assume parent does parameter checks */
unsigned int len = (unsigned int)strlen(refS);


size_t len = strlen(refS);

Sinan


I like using unsigned ints when I use bit shifts :
although I suppose I could cast it at that point so, yes, size_t is
better. But is it wrong to cast the return from strlen? What does
size_t "equate" to in the standard?
Mar 20 '06 #12

P: n/a
On 2006-03-20, santosh <sa*********@gmail.com> wrote:
Richard G. Riley wrote:

Its all an else in this case since you returned.
I'm sorry, I don't get what you're trying to say here.


the "do" might just as well be in the "else" or you could not bother
with the else, e.g

if(error)
return whatever;
do other stuff;

Your version is more concise. But why not use the proper type for
strlen()'s return value? What's the reason to cast it to an unsigned
int?
See other reply. size_t is probably better, but Im waiting on a reply
to a question - its another thing Ive probably been lazy about in the
past : I just like unsigned ints for this type of stuff since then I
dont have to cast continually for printfs, logging but am willing to
be corrected if its really evil :-; Also unisgned ints are more "in
your face" for bit shifts to divide by powers of 2.

Thanks for the feedback.

Mar 20 '06 #13

P: n/a
"Richard G. Riley" <rg****@gmail.com> wrote in news:did3f3-k3j.ln1
@fujitsu.mydomain.com:
On 2006-03-20, A. Sinan Unur <1u**@llenroc.ude.invalid> wrote:
"Richard G. Riley" <rg****@gmail.com> wrote in news:tub3f3-03i.ln1
@fujitsu.mydomain.com:
void reverse(char * refS){
/* assume parent does parameter checks */
unsigned int len = (unsigned int)strlen(refS);
size_t len = strlen(refS);

....
I like using unsigned ints when I use bit shifts :
although I suppose I could cast it at that point so, yes, size_t is
better. But is it wrong to cast the return from strlen? What does
size_t "equate" to in the standard?


7.17 Common definitions <stddef.h>
1 The following types and macros are defined in the standard header
<stddef.h>. Some are also defined in other headers, as noted in their
respective subclauses.

2 The types are

....

size_t

which is the unsigned integer type of the result of the sizeof operator;

Sinan
--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
Mar 20 '06 #14

P: n/a
"Richard G. Riley" <rg****@gmail.com> writes:
On 2006-03-20, A. Sinan Unur <1u**@llenroc.ude.invalid> wrote:
"Richard G. Riley" <rg****@gmail.com> wrote in news:tub3f3-03i.ln1
@fujitsu.mydomain.com:
void reverse(char * refS){
/* assume parent does parameter checks */
unsigned int len = (unsigned int)strlen(refS);


size_t len = strlen(refS);

Sinan


I like using unsigned ints when I use bit shifts :
although I suppose I could cast it at that point so, yes, size_t is
better. But is it wrong to cast the return from strlen? What does
size_t "equate" to in the standard?


size_t is an unsigned integer type.

"Is it wrong to cast" is the wrong question. The right question is,
"Is this cast really necessary?". If it isn't, lose it. In this
case, there will be an implicit conversion that will do exactly the
same thing the cast does -- except that the implicit conversion will
always get the type right, whereas it's easy to use the wrong type in
an explicit cast.

All casts should be viewed with suspicion.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 20 '06 #15

P: n/a
sudharsan wrote:

could any one please give me a code to reverse a string of more
than 1MB .???


Sure. It may not be very fast for such a size, but assuming you
can create the string in the first place it should reverse it.
Don't forget to #include <string.h>. This avoids extra calls on
strlen by returning its value.

/* ======================= */
/* reverse string in place */
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 20 '06 #16

P: n/a
In article <cv************@fujitsu.mydomain.com>,
Richard G. Riley <rg****@gmail.com> wrote:
could any one please give me a code to reverse a string of more than
1MB .???
wtrite some code to reverse a string that works for 0,1,2 and 3
characters and it will probably work for a megabyte of them too. size
is no indicator of complexity.


Here's a program that reverses a string in an obvious way. It
reverses the substring after the first character and then puts the
first character onto the end, taking care to free the temporary
strings as soon as possible. It seems to work nicely for 0, 1, 2, and
3 characters.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char *nrev(const char *old);

int main(int argc, char **argv)
{
char *old, *new;
int l;

if(argc != 2)
{
fprintf(stderr, "usage: nrev length\n");
return 2;
}

l = atoi(argv[1]);
old = malloc(l + 1);
memset(old, 'x', l);
old[l] = 0;

new = nrev(old);

return 0;
}

static char *nrev(const char *old)
{
int len;
char *new;
char *t;

len = strlen(old);
new = malloc(len+1);

if(len > 0)
{
t = nrev(old+1);
memcpy(new, t, len-1);
free(t);
new[len-1] = old[0];
}

new[len] = 0;
return new;
}

-- Richard
Mar 20 '06 #17

P: n/a
On 2006-03-20, sudharsan <su*********@gmail.com> wrote:
could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance


What have you tried that doesn't work with a 1MB string?

A favorite job interview question is to reverse a string of arbitrary
size in place.

Draw yourself a picture of an arbitrary string and figure out how you'd
reverse it in place. You should only need a single temporary.
Mar 21 '06 #18

P: n/a
In article <rc********************@comcast.com>,
Charles Krug <cd****@aol.com> wrote:
Draw yourself a picture of an arbitrary string and figure out how you'd
reverse it in place. You should only need a single temporary.


You don't even need one - you can use the nul at the end of the
string...

-- Richard
Mar 21 '06 #19

P: n/a
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <rc********************@comcast.com>,
Charles Krug <cd****@aol.com> wrote:
Draw yourself a picture of an arbitrary string and figure out how you'd
reverse it in place. You should only need a single temporary.


You don't even need one - you can use the nul at the end of the
string...


Yes, if saving a single temporary object is worth writing abominably
ugly code.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 21 '06 #20

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
Draw yourself a picture of an arbitrary string and figure out how you'd
reverse it in place. You should only need a single temporary.


You don't even need one - you can use the nul at the end of the
string...


Yes, if saving a single temporary object is worth writing abominably
ugly code.


I repeatedly see people people denouncing the use of smilies on the
grounds that intelligent readers don't need them, but I'm beginning
to doubt it.

-- Richard
Mar 21 '06 #21

P: n/a
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
Draw yourself a picture of an arbitrary string and figure out how you'd
reverse it in place. You should only need a single temporary.

You don't even need one - you can use the nul at the end of the
string...


Yes, if saving a single temporary object is worth writing abominably
ugly code.


I repeatedly see people people denouncing the use of smilies on the
grounds that intelligent readers don't need them, but I'm beginning
to doubt it.


You think the articles from folks who think XOR is a good way to
swap two variables are all jokes then?
--
"...Almost makes you wonder why Heisenberg didn't include postinc/dec operators
in the uncertainty principle. Which of course makes the above equivalent to
Schrodinger's pointer..."
--Anthony McDonald
Mar 21 '06 #22

P: n/a
On Mon, 20 Mar 2006 19:48:05 UTC, CBFalconer <cb********@yahoo.com>
wrote:
sudharsan wrote:

could any one please give me a code to reverse a string of more
than 1MB .???


Sure. It may not be very fast for such a size, but assuming you
can create the string in the first place it should reverse it.
Don't forget to #include <string.h>. This avoids extra calls on
strlen by returning its value.

/* ======================= */
/* reverse string in place */
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */


Too complicated because superflous statements.

strictly conforming, best optimised:

/* reverse a string of 0 or more bytes in length in place */
/* caller is responsible for NOT calling with a null pointer */
/* or NOT calling with a string constant */
/* return: size of the string to reverse */

size_t revstring(void *p) {
char *l = p;
/* size is needed only to get it returned!
r should be calculated: = l + strlen(l) -1; */
size_t size = strlen(p);
char *r = l + size - 1;
char c;

while (l < r) { /* when l >= r we're done */
c = *l;
*l++ = *r;
*r-- = c;
}
return size;
}

Remarks:
size < 2: nothing is done, except setup the variables.
size is uneven: the middlest char lefts untouched as it has no
counterpart

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Mar 21 '06 #23

P: n/a
Herbert Rosenau wrote:
CBFalconer <cb********@yahoo.com> wrote:
sudharsan wrote:

could any one please give me a code to reverse a string of more
than 1MB .???
Sure. It may not be very fast for such a size, but assuming you
can create the string in the first place it should reverse it.
Don't forget to #include <string.h>. This avoids extra calls on
strlen by returning its value.

/* ======================= */
/* reverse string in place */
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */


Too complicated because superflous statements.

strictly conforming, best optimised:

/* reverse a string of 0 or more bytes in length in place */
/* caller is responsible for NOT calling with a null pointer */
/* or NOT calling with a string constant */
/* return: size of the string to reverse */

size_t revstring(void *p) {


The type of a string is char *, not void *.
char *l = p;
/* size is needed only to get it returned!
r should be calculated: = l + strlen(l) -1; */
size_t size = strlen(p);
char *r = l + size - 1;
Undefined behaviour when the string is empty.
char c;

while (l < r) { /* when l >= r we're done */
c = *l;
*l++ = *r;
*r-- = c;
}
return size;
}

Remarks:
size < 2: nothing is done, except setup the variables.
size is uneven: the middlest char lefts untouched as it has no
counterpart


Both of which characteristics are present in my code, without the
undefined behaviour. In fact for size < 2 nothing whatsoever is
done in my code.

--
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
"We have always known that heedless self-interest was bad
morals. We now know that it is bad economics" - FDR
Mar 22 '06 #24

P: n/a
CBFalconer wrote:
Herbert Rosenau wrote:
CBFalconer <cb********@yahoo.com> wrote:
sudharsan wrote:
could any one please give me a code to reverse a string of more
than 1MB .???
Sure. It may not be very fast for such a size, but assuming you
can create the string in the first place it should reverse it.
Don't forget to #include <string.h>. This avoids extra calls on
strlen by returning its value.

/* ======================= */
/* reverse string in place */
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

Too complicated because superflous statements.

strictly conforming, best optimised:

/* reverse a string of 0 or more bytes in length in place */
/* caller is responsible for NOT calling with a null pointer */
/* or NOT calling with a string constant */
/* return: size of the string to reverse */

size_t revstring(void *p) {


The type of a string is char *, not void *.
char *l = p;
/* size is needed only to get it returned!
r should be calculated: = l + strlen(l) -1; */
size_t size = strlen(p);
char *r = l + size - 1;


Undefined behaviour when the string is empty.
char c;

while (l < r) { /* when l >= r we're done */
c = *l;
*l++ = *r;
*r-- = c;
}
return size;
}

Remarks:
size < 2: nothing is done, except setup the variables.
size is uneven: the middlest char lefts untouched as it has no
counterpart


Both of which characteristics are present in my code, without the
undefined behaviour. In fact for size < 2 nothing whatsoever is
done in my code.


IIRC, Bentley's Programming Pearls has several algorithms
including a clever one. I think he even gives the code in
the back of the book.

--
Julian V. Noble
Professor Emeritus of Physics

http://galileo.phys.virginia.edu/~jvn/

"For there was never yet philosopher that could endure the
toothache patiently."

-- Wm. Shakespeare, Much Ado about Nothing. Act v. Sc. 1.
Mar 23 '06 #25

P: n/a
Vladimir S. Oka wrote:

sudharsan opined:
could any one please give me a code to reverse a string of more than
1MB .???


Why do you think it would be different to
the one for strings less than 1MB?


Because a conforming implementation of C,
is not required to be able to translate and execute
a program which excedes the minimum translation limits.

strlen isn't required to work right on a string that long.

The business in the standard about the ptrdiff_t type
not being guaranteed to be able to do its job,
is most appropriately applied to code with objects
which exceede the translation limits.
I've seen strlen implemented on this newsgroup
in ways which depend upon the ptrdiff_t type
being able to do its job.

--
pete
Mar 23 '06 #26

P: n/a

pete wrote:
Vladimir S. Oka wrote:

sudharsan opined:
could any one please give me a code to reverse a string of more than
1MB .???


Why do you think it would be different to
the one for strings less than 1MB?


Because a conforming implementation of C,
is not required to be able to translate and execute
a program which excedes the minimum translation limits.

strlen isn't required to work right on a string that long.

The business in the standard about the ptrdiff_t type
not being guaranteed to be able to do its job,
is most appropriately applied to code with objects
which exceede the translation limits.
I've seen strlen implemented on this newsgroup
in ways which depend upon the ptrdiff_t type
being able to do its job.


All you say is fine, but I don't see how it applies to your quote of
what I said.

Unless you took to the word "code" in the OP to meant "exact
implementation detail", while I read it as "the [general] method". In
that case we actually agree, but are talking about diferent parts of
the problem.

The fact that you may have to implement your own `strlen()`, or
whatever, to me, does not change the way this is done.

--
BR, Vladimir

Mar 23 '06 #27

P: n/a
Vladimir S. Oka wrote:
The fact that you may have to implement your own `strlen()`, or
whatever, to me, does not change the way this is done.


To me, whether or not you have to implement your own strlen,
does change the way this is done.

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);
while (t > s) {
swap = *t;
*t-- = *s;
*s++ = swap;
}
}
return p;
}

--
pete
Mar 23 '06 #28

P: n/a
CBFalconer wrote:

Herbert Rosenau wrote:
CBFalconer <cb********@yahoo.com> wrote:
sudharsan wrote:

could any one please give me a code to reverse a string of more
than 1MB .???

Sure. It may not be very fast for such a size, but assuming you
can create the string in the first place it should reverse it.
Don't forget to #include <string.h>. This avoids extra calls on
strlen by returning its value.

/* ======================= */
/* reverse string in place */
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
for size < 2 nothing whatsoever is
done in my code.


Not so.
You have a function call to strlen even when (size < 2).

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (s[0] != '\0' && s[1] != '\0') {
t = s + 1 + strlen(s + 2);
do {
swap = *t;
*t-- = *s;
*s++ = swap;
} while (t > s);
}
return p;
}

--
pete
Mar 23 '06 #29

P: n/a
pete wrote:
Vladimir S. Oka wrote:
The fact that you may have to implement your own `strlen()`, or
whatever, to me, does not change the way this is done.


To me, whether or not you have to implement your own strlen,
does change the way this is done.

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);
while (t > s) {
swap = *t;
*t-- = *s;
*s++ = swap;
}
}
return p;
}


Er - that doesn't work. Check the boundary conditions. See my
revstring function elsethread. It is also a design mistake to
return the string pointer, because of such innocent looking code
as:

printf("%s\n%s\n", str, str_rev(str));

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 24 '06 #30

P: n/a
pete wrote:
CBFalconer wrote:
Herbert Rosenau wrote:
CBFalconer <cb********@yahoo.com> wrote:
sudharsan wrote:
>
> could any one please give me a code to reverse a string of more
> than 1MB .???

Sure. It may not be very fast for such a size, but assuming you
can create the string in the first place it should reverse it.
Don't forget to #include <string.h>. This avoids extra calls on
strlen by returning its value.

/* ======================= */
/* reverse string in place */
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);

for size < 2 nothing whatsoever is
done in my code.


Not so.
You have a function call to strlen even when (size < 2).

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (s[0] != '\0' && s[1] != '\0') {
t = s + 1 + strlen(s + 2);
do {
swap = *t;
*t-- = *s;
*s++ = swap;
} while (t > s);
}
return p;
}


I think a strlen call is somewhat better than all that, and you end
up calling strlen anyhow. If strlen returns 1 or less it won't
take very long to execute. I haven't bothered to check the end
conditions of your code, but based on your other posting I am
suspicious.

Please mark areas where you snip material. ... will do.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 24 '06 #31

P: n/a
pete opined:
Vladimir S. Oka wrote:
The fact that you may have to implement your own `strlen()`, or
whatever, to me, does not change the way this is done.
To me, whether or not you have to implement your own strlen,
does change the way this is done.


How so?
char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);
What's wrong with:

t = s + my_strlen(s + 1);
while (t > s) {
swap = *t;
*t-- = *s;
*s++ = swap;
}
}
return p;
}


It seems Chuck thinks the above doesn't work -- I didn't check myself.

--
BR, Vladimir

Not all men who drink are poets.
Some of us drink because we aren't poets.

Mar 24 '06 #32

P: n/a
CBFalconer wrote:

pete wrote:
CBFalconer wrote:
Herbert Rosenau wrote:
CBFalconer <cb********@yahoo.com> wrote:
> sudharsan wrote:
>>
>> could any one please give me a code to reverse a string of more
>> than 1MB .???
>
> Sure. It may not be very fast for such a size, but assuming you
> can create the string in the first place it should reverse it.
> Don't forget to #include <string.h>. This avoids extra calls on
> strlen by returning its value.
>
> /* ======================= */
> /* reverse string in place */
> size_t revstring(char *stg)
> {
> char *last, temp;
> size_t lgh;
>
> lgh = strlen(stg);
for size < 2 nothing whatsoever is
done in my code.


Not so.
You have a function call to strlen even when (size < 2).

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (s[0] != '\0' && s[1] != '\0') {
t = s + 1 + strlen(s + 2);
do {
swap = *t;
*t-- = *s;
*s++ = swap;
} while (t > s);
}
return p;
}


I think a strlen call is somewhat better than all that,


I'm not saying that it isn't.
and you end up calling strlen anyhow.
Only when the length is greater than one.
If strlen returns 1 or less it won't
take very long to execute.
True.
I haven't bothered to check the end
conditions of your code,
but based on your other posting I am
suspicious.
Check them.
Please mark areas where you snip material. ... will do.


I hadn't thought about it at first,
but since your function returned length and mine didn't,
they were not really the same.

size_t str_rev(char *s)
{
char *t, swap;
size_t length;

if (s[0] != '\0') {
if (s[1] != '\0') {
length = strlen(s);
t = s + length - 1;
do {
swap = *t;
*t-- = *s;
*s++ = swap;
} while (t > s);
} else {
length = 1;
}
} else {
length = 0;
}
return length;
}

--
pete
Mar 24 '06 #33

P: n/a
CBFalconer wrote:

pete wrote:
Vladimir S. Oka wrote:
The fact that you may have to implement your own `strlen()`, or
whatever, to me, does not change the way this is done.
To me, whether or not you have to implement your own strlen,
does change the way this is done.

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);
while (t > s) {
swap = *t;
*t-- = *s;
*s++ = swap;
}
}
return p;
}


Er - that doesn't work. Check the boundary conditions.


I can't find a problem.

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);
while (t > s) {
swap = *t;
*t-- = *s;
*s++ = swap;
}
}
return p;
}

int main(void)
{
char string[] = "abc";

puts(str_rev(string));
str_rev(string);
string[2] = '\0';
puts(str_rev(string));
str_rev(string);
string[1] = '\0';
str_rev(string);
string[0] = '\0';
puts(str_rev(string));
return 0;
}

/* END new.c */

See my
revstring function elsethread. It is also a design mistake to
return the string pointer, because of such innocent looking code
as:

printf("%s\n%s\n", str, str_rev(str));


--
pete
Mar 24 '06 #34

P: n/a
Vladimir S. Oka wrote:

pete opined:
Vladimir S. Oka wrote:
The fact that you may have to implement your own `strlen()`, or
whatever, to me, does not change the way this is done.


To me, whether or not you have to implement your own strlen,
does change the way this is done.


How so?
char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);


What's wrong with:


Why would you write your own strlen to reverse a string,
instead of using the one in the standard library?

--
pete
Mar 24 '06 #35

P: n/a

pete wrote:
Vladimir S. Oka wrote:

pete opined:
Vladimir S. Oka wrote:

> The fact that you may have to implement your own `strlen()`, or
> whatever, to me, does not change the way this is done.

To me, whether or not you have to implement your own strlen,
does change the way this is done.
How so?
char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);


What's wrong with:


Why would you write your own strlen to reverse a string,
instead of using the one in the standard library?


I was replying to what you said earlier:
Because a conforming implementation of C,
is not required to be able to translate and execute
a program which excedes the minimum translation limits. strlen isn't required to work right on a string that long.
Which you snipped.

To expand: if `strlen()` won't necessarilly work on huge strings (your
assertion above, I didn't check the Standard), then rolling your own
may help.

--
BR, Vladimir

--
pete


Mar 24 '06 #36

P: n/a
Vladimir S. Oka wrote:
To expand: if `strlen()` won't necessarilly work on huge strings (your
assertion above, I didn't check the Standard),
then rolling your own may help.


That's the answer to your original question:

"Why do you think it would be different to the one
for strings less than 1MB?"

This is a c language newsgroup, not an algorithm newsgroup.

--
pete
Mar 24 '06 #37

P: n/a
pete wrote:

CBFalconer wrote:
Er - that doesn't work. Check the boundary conditions.


I can't find a problem.

int main(void)
{
char string[] = "abc";

puts(str_rev(string));
str_rev(string);
string[2] = '\0';
puts(str_rev(string));
str_rev(string);
string[1] = '\0';
str_rev(string);
string[0] = '\0';
puts(str_rev(string));
return 0;
}


This might be better, unless I don't understand
what you mean by "boundary conditions".

int main(void)
{
char string[sizeof "abc"] = "";
size_t n;

for (n = 0; n != sizeof "abc" - 1; ++n) {
str_rev(string);
puts(string);
str_rev(string);
string[n] = "abc"[n];
}
str_rev(string);
puts(string);
return 0;
}

--
pete
Mar 24 '06 #38

P: n/a
pete wrote:
>pete wrote:
>>Vladimir S. Oka wrote:
>
>>>sudharsan opined:
>
>>>> could any one please give me a code to reverse a string of more than
>>>> 1MB .???
>
>>>Why do you think it would be different to
>>>the one for strings less than 1MB?
>
>>Because a conforming implementation of C,
>>is not required to be able to translate and execute
>>a program which excedes the minimum translation limits.
>
>>strlen isn't required to work right on a string that long.
>
>>The business in the standard about the ptrdiff_t type
>>not being guaranteed to be able to do its job,
>>is most appropriately applied to code with objects
>>which exceede the translation limits.
>>I've seen strlen implemented on this newsgroup
>>in ways which depend upon the ptrdiff_t type
>>being able to do its job.
>
>All you say is fine, but I don't see how it applies to your quote of
>what I said.
>
>Unless you took to the word "code" in the OP to meant "exact
>implementation detail", while I read it as "the [general] method". In
>that case we actually agree, but are talking about diferent parts of
>the problem.
>
>The fact that you may have to implement your own `strlen()`, or
>whatever, to me, does not change the way this is done.

To me, whether or not you have to implement your own strlen,
does change the way this is done.

How so?

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);

What's wrong with:

t = s + my_strlen(s + 1);

while (t > s) {
swap = *t;
*t-- = *s;
*s++ = swap;
}
}
return p;

}

It seems Chuck thinks the above doesn't work -- I didn't check myself.


Why would you write your own strlen to reverse a string,
instead of using the one in the standard library?

I was replying to what you said earlier:
Because a conforming implementation of C,
is not required to be able to translate and execute
a program which excedes the minimum translation limits.
strlen isn't required to work right on a string that long.


Which you snipped.

To expand: if `strlen()` won't necessarilly work on huge strings (your
assertion above, I didn't check the Standard), then rolling your own
may help.


That's the answer to your original question:

"Why do you think it would be different to the one
for strings less than 1MB?"

This is a c language newsgroup, not an algorithm newsgroup.


I have now, painstaikingly, re-built the whole exchange. Without
snipping *anything* will you, please, point to exact things that I said
that you have an issue with, and exactly why?

--
BR, Vladimir

Mar 24 '06 #39

P: n/a
Vladimir S. Oka wrote:

pete wrote:
>>pete wrote:
>>>Vladimir S. Oka wrote:
>>
>>>>sudharsan opined:
>>
>>>>> could any one please give me a code to reverse a string of more than
>>>>> 1MB .???
>>
>>>>Why do you think it would be different to
>>>>the one for strings less than 1MB?
>>
>>>Because a conforming implementation of C,
>>>is not required to be able to translate and execute
>>>a program which excedes the minimum translation limits.
>>
>>>strlen isn't required to work right on a string that long.
>>
>>>The business in the standard about the ptrdiff_t type
>>>not being guaranteed to be able to do its job,
>>>is most appropriately applied to code with objects
>>>which exceede the translation limits.
>>>I've seen strlen implemented on this newsgroup
>>>in ways which depend upon the ptrdiff_t type
>>>being able to do its job.
>>
>>All you say is fine,
>>but I don't see how it applies to your quote of
>>what I said.
>>
>>Unless you took to the word "code" in the OP to meant "exact
>>implementation detail",
>>while I read it as "the [general] method". In
I took it to mean exact "implementation detail"
and the reason that I did,
is because the exact implementation detail
depends on the length of the string.

The difference in methodology,
depending on the length of the string, is a c language issue.
To me, "the [general] method" sounds like an algorithm issue.
This is a c language newsgroup, not an algorithm newsgroup.
that case we actually agree,
>>but are talking about diferent parts of
>>the problem.
>>
>>The fact that you may have to implement your own `strlen()`, or
>>whatever, to me, does not change the way this is done.
>
>To me, whether or not you have to implement your own strlen,
>does change the way this is done.

How so?

> char *str_rev(char *s)
> {
> char *t, swap;
> char *const p = s;
>
> if (*s != '\0') {
> t = s + strlen(s + 1);

What's wrong with:

t = s + my_strlen(s + 1);

> while (t > s) {
> swap = *t;
> *t-- = *s;
> *s++ = swap;
> }
> }
> return p;
>
> }

It seems Chuck thinks the above doesn't work
-- I didn't check myself.
Too bad that you didn't.
You could have opinion of your own about the code, if you had.

Why would you write your own strlen to reverse a string,
instead of using the one in the standard library?

I was replying to what you said earlier:

Because a conforming implementation of C,
is not required to be able to translate and execute
a program which excedes the minimum translation limits.
strlen isn't required to work right on a string that long.

Which you snipped.

To expand:
if `strlen()` won't necessarilly work on huge strings (your
assertion above, I didn't check the Standard), then rolling your own
may help.


That's the answer to your original question:

"Why do you think it would be different to the one
for strings less than 1MB?"

This is a c language newsgroup, not an algorithm newsgroup.


I have now, painstaikingly, re-built the whole exchange. Without
snipping *anything* will you, please,
point to exact things that I said
that you have an issue with, and exactly why?


--
pete
Mar 24 '06 #40

P: n/a
pete opined:
Vladimir S. Oka wrote:

pete wrote:
>>>>>pete wrote:
>>>>>>Vladimir S. Oka wrote:
>>>>>
>>>>>>>sudharsan opined:
>>>>>
>>>>>>>> could any one please give me a code to reverse a string of
>>>>>>>> more than 1MB .???
>>>>>
>>>>>>>Why do you think it would be different to
>>>>>>>the one for strings less than 1MB?
>>>>>
>>>>>>Because a conforming implementation of C,
>>>>>>is not required to be able to translate and execute
>>>>>>a program which excedes the minimum translation limits.
>>>>>
>>>>>>strlen isn't required to work right on a string that long.
>>>>>
>>>>>>The business in the standard about the ptrdiff_t type
>>>>>>not being guaranteed to be able to do its job,
>>>>>>is most appropriately applied to code with objects
>>>>>>which exceede the translation limits.
>>>>>>I've seen strlen implemented on this newsgroup
>>>>>>in ways which depend upon the ptrdiff_t type
>>>>>>being able to do its job.
>>>>>
>>>>>All you say is fine,
>>>>>but I don't see how it applies to your quote of
>>>>>what I said.
>>>>>
>>>>>Unless you took to the word "code" in the OP to meant "exact
>>>>>implementation detail",
>>>>>while I read it as "the [general] method". In
I took it to mean exact "implementation detail"
and the reason that I did,
is because the exact implementation detail
depends on the length of the string.
In which case we actually agree.
The difference in methodology,
depending on the length of the string, is a c language issue.
To me, "the [general] method" sounds like an algorithm issue.
This is a c language newsgroup, not an algorithm newsgroup.


Yes, but in this case they seem to be intertwined since C language
features have a bearing on the method chosen. In any case, I tend not
to /discuss/ algorithm issues here, but I do offer an odd idea.
>>>>>that case we actually agree,
>>>>>but are talking about diferent parts of
>>>>>the problem.
>>>>>
>>>>>The fact that you may have to implement your own `strlen()`, or
>>>>>whatever, to me, does not change the way this is done.
>>>>
>>>>To me, whether or not you have to implement your own strlen,
>>>>does change the way this is done.
>>>
>>>How so?
>>>
>>>> char *str_rev(char *s)
>>>> {
>>>> char *t, swap;
>>>> char *const p = s;
>>>>
>>>> if (*s != '\0') {
>>>> t = s + strlen(s + 1);
>>>
>>>What's wrong with:
>>>
>>> t = s + my_strlen(s + 1);
>>>
>>>> while (t > s) {
>>>> swap = *t;
>>>> *t-- = *s;
>>>> *s++ = swap;
>>>> }
>>>> }
>>>> return p;
>>>>
>>>> }
>>>
>>>It seems Chuck thinks the above doesn't work
>>> -- I didn't check myself.
Too bad that you didn't.
You could have opinion of your own about the code, if you had.


Admittedly, my comment was unnecessary, to say the least, and certainly
vacuous. I did not express /any/ opinion about your code, though.
Let's say I made a clumsy, and unnecessary reference to another post
that did.
>>Why would you write your own strlen to reverse a string,
>>instead of using the one in the standard library?
>>
>>I was replying to what you said earlier:
>>
>>> Because a conforming implementation of C,
>>> is not required to be able to translate and execute
>>> a program which excedes the minimum translation limits.
>>> strlen isn't required to work right on a string that long.
>>
>>Which you snipped.
>>
>>To expand:
>>if `strlen()` won't necessarilly work on huge strings (your
>>assertion above, I didn't check the Standard), then rolling your
>>own may help.
>
>That's the answer to your original question:
>
>"Why do you think it would be different to the one
> for strings less than 1MB?"
>
>This is a c language newsgroup, not an algorithm newsgroup.


I have now, painstaikingly, re-built the whole exchange. Without
snipping *anything* will you, please,
point to exact things that I said
that you have an issue with, and exactly why?


If you still feel I was grossly off-topic, I apologise, but beg to
disagree, and let's leave it at that.

--
BR, Vladimir

So far as I can remember, there is not one word in the Gospels in
praise of intelligence.
-- Bertrand Russell

Mar 24 '06 #41

P: n/a
pete wrote:
CBFalconer wrote:
pete wrote:
Vladimir S. Oka wrote:

The fact that you may have to implement your own `strlen()`, or
whatever, to me, does not change the way this is done.

To me, whether or not you have to implement your own strlen,
does change the way this is done.

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);
while (t > s) {
swap = *t;
*t-- = *s;
*s++ = swap;
}
}
return p;
}


Er - that doesn't work. Check the boundary conditions.


I can't find a problem.


I spoke too quickly. It does work. Sorry.

Boundary conditions mean just that, in this case 0 length strings,
or because of the initial elimination 1 length strings.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Mar 24 '06 #42

P: n/a
pete wrote:
CBFalconer wrote:
.... snip ...
I hadn't thought about it at first,
but since your function returned length and mine didn't,
they were not really the same.

size_t str_rev(char *s)
{
char *t, swap;
size_t length;

if (s[0] != '\0') {
if (s[1] != '\0') {
length = strlen(s);
t = s + length - 1;
do {
swap = *t;
*t-- = *s;
*s++ = swap;
} while (t > s);
} else {
length = 1;
}
} else {
length = 0;
}
return length;
}


size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

Just for side by side comparison. The fundamental formatting is
the same, the only basic difference being that I use multiple
instructions per line to implement the "tradechars" operation. I
maintain my code is clearer, and probably generates more compact
object code, barring a very smart optimizer. It is certainly more
succint.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Mar 24 '06 #43

P: n/a
Ben Pfaff wrote
(in article <87************@benpfaff.org>):
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
> Draw yourself a picture of an arbitrary string and figure out how you'd
> reverse it in place. You should only need a single temporary.

You don't even need one - you can use the nul at the end of the
string...

Yes, if saving a single temporary object is worth writing abominably
ugly code.


I repeatedly see people people denouncing the use of smilies on the
grounds that intelligent readers don't need them, but I'm beginning
to doubt it.


You think the articles from folks who think XOR is a good way to
swap two variables are all jokes then?


They're always funny, even if the poster doesn't realize he is
making a joke of himself. :-)
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Mar 24 '06 #44

P: n/a
CBFalconer wrote:
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

Just for side by side comparison. The fundamental formatting is
the same, the only basic difference being that I use multiple
instructions per line to implement the "tradechars" operation. I
maintain my code is clearer, and probably generates more compact
object code, barring a very smart optimizer. It is certainly more
succint.


It swaps the middle element
of an odd length string with itself!
Holy redundancy Batman!
You have not a single thought to efficiency,
expressed in this code.

--
pete
Mar 25 '06 #45

P: n/a
pete wrote:

CBFalconer wrote:
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

Just for side by side comparison. The fundamental formatting is
the same, the only basic difference being that I use multiple
instructions per line to implement the "tradechars" operation. I
maintain my code is clearer, and probably generates more compact
object code, barring a very smart optimizer. It is certainly more
succint.


It swaps the middle element
of an odd length string with itself!
Holy redundancy Batman!
You have not a single thought to efficiency,
expressed in this code.


size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh - 1;
do {
temp = *stg; *stg++ = *last; *last-- = temp;
} while (last > stg);
}
return lgh;
} /* revstring */
--
pete
Mar 25 '06 #46

P: n/a
pete wrote:
CBFalconer wrote:
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

Just for side by side comparison. The fundamental formatting is
the same, the only basic difference being that I use multiple
instructions per line to implement the "tradechars" operation. I
maintain my code is clearer, and probably generates more compact
object code, barring a very smart optimizer. It is certainly more
succint.


It swaps the middle element of an odd length string with itself!
Holy redundancy Batman!
You have not a single thought to efficiency, expressed in this code.


If that really bothers you change last-- to --last.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Mar 25 '06 #47

P: n/a
pete wrote:
.... snip ...
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh - 1;
do {
temp = *stg; *stg++ = *last; *last-- = temp;
} while (last > stg);
}
return lgh;
} /* revstring */


I think that works too, and is clear.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Mar 25 '06 #48

This discussion thread is closed

Replies have been disabled for this discussion.