To reverse a string

V

Vladimir S. Oka

pete opined:
Vladimir said:
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.
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.

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
 
C

CBFalconer

pete said:
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.
 
C

CBFalconer

pete said:
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.
 
R

Randy Howard

Ben Pfaff wrote
(in article said:
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. :)
 
P

pete

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

pete

pete said:
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 */
 
C

CBFalconer

pete said:
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.
 
C

CBFalconer

pete said:
.... 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.
 

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
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top