J
joecook
dbtouch wrote:
In other words, under the more-or-less strict interpretation of the
terms you used in the statement of your problem, it has no solution. You
need to clarify what is really needed.
I disagree. I think the solution is obvious. Here is an example of
reversing a string using no auxillary memory at all (no recursion, no
temporaries). I use the Null terminator location as my swap space.
In fact, I do have a loop temporary, but you could easily conceive
this could be eliminated using templates, and a compile time constant
size (this doesn't not violate any of the conditions set forth).
Also, this is only the half that works for even-length strings. I'll
leave the odd lengths as an exercise to the reader.
I only "walk" through the string one time
void reverse(char* input, int size) // size does not include the null
terminator
{
// Walk through the first half.
for(int i=0; i<size/2; ++i)
{
input[size-1] = input;
intput = input[size-i-1];
}
// Walk through the rest
for(int i=size/2+1; i<size; ++i)
{
input = input[i+1];
}
input[size] = "\0"; // restore the null terminator
}
int main()
{
char a[] = "Hello!";
reverse(a,6);
}
Joe Cook