How do I reverse a string with a single variable for swapping

W

Walter Roberson

complex answers for a simple problem. Solution is:-

The solution for what? It's better to quote context, as most of us
do NOT use google groups to read postings.

int main(void)
{
char *name = "member";
int len = strlen(name);

strlen() is not prototyped without said:
int i;
len-=1;
char tmp; //single variable

Using // is not valid in C89.
Making a declaration after an executable statement is not valid in C89.
for (i=0; i<=len; i++,--len)
{
tmp = name;
name = name[len]


Missing semicolon is not valid in C89 or C99.
Writing to a string literal is implementation defined in C89 and C99.
name[len] = tmp;
}

Failure to return a value from main is implementation defined in C89.

All variables go out of scope at this point, but nothing in this
code has produced any side-effect such as I/O. The compiler could
potentially optimize all the code out entirely.

complex answers for a simple problem. Solution is:-

Looks like that wasn't the solution after all. Was this due to a
misundertanding of the problem, or due to a misunderstanding of how
to program in C ?
 
M

Michael Mair

Walter said:

[snip: Derision of "solution" by ashu]
Looks like that wasn't the solution after all. Was this due to a
misundertanding of the problem, or due to a misunderstanding of how
to program in C ?

Walter, reading your reply, I realised that you have become just
as acerbic as the average clc regular... ;-)

Cheers
Michael
 
W

Walter Roberson

Walter said:
[snip: Derision of "solution" by ashu]

That wasn't derision, that was pointing out problems.
Walter, reading your reply, I realised that you have become just
as acerbic as the average clc regular... ;-)

Not yet -- but when someone posts as if they know better than anyone else,
and claims that they have the solution that everyone else has missed,
then they should be right.

Walter, reading your reply, I realised that you have become just
as acerbic as the average clc regular... ;-)

It must be something going around. Even Keith is getting a bit testy lately.
 
J

Jordan Abel

Fair enough. If the original question was to reverse the string
"in place", i.e. without using an additional strlen(str) of space
to build an entirely new string, that's a perfectly good question.
The question I was flaming about (perhaps ill-advisedly) is the
one where you're supposed to swap using 3 obscure XOR's instead
of 3 perfectly normal assignments and a temporary.

You can also do it with addition and subtraction:

x += y; y -= x; x += y; y = -y;

There's a potential of overflow if you're using a signed type, but
then, the XOR version has a potential of a trap representation in an
intermediate step.
 
F

Flash Gordon

Ashu could have done the subtraction on the initial assignment of len.
Using // is not valid in C89.
Making a declaration after an executable statement is not valid in C89.

True. Also, even when using C99 they are a bad idea for Usenet posts
because they do not survive line wrapping.

You missed that this should be i<len not i<=len because there is no
point in swapping a character with itself.

I would not have bothered posting just for this, but since I'm posting
anyway...
{
tmp = name;
name = name[len]


Missing semicolon is not valid in C89 or C99.
Writing to a string literal is implementation defined in C89 and C99.


I think you mean undefined behaviour, not implementation defined. One
nice thing about a number of modern C implementations is that this will
actually crash on them, although that is not required of course.

This was the main point of and reason for my post.
name[len] = tmp;
}

Failure to return a value from main is implementation defined in C89.

<snip>
 
K

Keith Thompson

Can you write code to reverse a string, at all?
If so, good. You could post it, and we might have some
suggestions on how to improve it.

Ignore the ridiculous constraint about "using just a single
variable for swapping". Your instructor is an idiot for imposing
that constraint. The "trick" for using a single variable is one
you would have no reason to know, and which there's no good
reason to learn. Attempting to labor under that constraint would
teach you absolutely nothing useful, and would at worst lure you
into writing significantly suboptimal, broken code.

Just write the natural code, then wrap all the variables in a struct.
It satisfies the stated requirements (unless you consider the struct
members to be variables). And, of course, it's a completely
gratuitous use of a struct.
 

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,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top