how can I reverse a string with out using any extra memory

how can I reverse a string with out using any extra memory, thx

example in any programming langueages is fine..
musclejackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

InteractiveMindCommented:
I only think this is possible if the string is a static length. Otherwise, you'd have to use _some_ extra memory (such as integer's for iteration).
0
InteractiveMindCommented:
I also doubt that this can be done without creating new variables on the lower level.

For example, in Java, you could probably pull this off, but what really goes on under the hood, when you [for example] call the substring() function will result in more memory being used temporarily..

So, I think you need to be much more specific here.
0
dlwyatt82Commented:
InteractiveMind has a point... using *any* temporary variable of any type (even integers or pointers) would technically use extra memory.  I will assume that you meant "without creating a copy of the string".  Here's a C example, which is probably the simplest example utliizing char pointers:

char * str = strdup("Reverse me!");

char * p1 = str;
char * p2 = str;

while (*p2 != '\0') { p2++; }
p2--;

while (p2 > p1) {
  char tmp = *p2;
  *p2 = *p1;
  *p1 = tmp;
  p2--;
  p1++;
}

printf("Reversed string: %s\n", str);
free(str);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

brettmjohnsonCommented:
This is a common academic programming assignment, so I won't post the complete code.
dlwyatt82, has posted a brute-force version that does use extra memory ( the char tmp ).

Here is a very big hint:  consider using bitwise XOR on the characters of the string.
0
brettmjohnsonCommented:
As I mentioned, it is possible to do this assignment without creating a copy of the string, AND without using temporary variables.  I know for a fact that when this assignment is given, the use of temporary variables is "extra memory".  So dlwyatt82's "assumption" is false, and his solution is incorrect.  So if you copy someone else's [incorrect] work and submit it as your own, you will get the grade you deserve.
0
dlwyatt82Commented:
And your "assumption" that I have misunderstood his requirements may also be false.  Quite frankly, even allocating a pointer consumes "extra memory".

Would it have been any different if I chose to use the address of the null character at the end of the string to hold the characters during the swap?  Now there is no "char tmp" on the stack, but I'm still using 2 or 3 temporary char pointers (which take up as much or more memory as the char itself).

You're right, it's possible this may be an academic assignment where a professor is looking specifically for the answer of using XORs to reverse the contents of two binary variables.  But even then, you have to use SOME sort of temporary storage while you enumerate over the string.  And quite frankly, assignments like this (if the prof is looking for only one correct answer) are just a way to doom honest students to failure, while those who know how to use Google can copy down the answer.  Most people don't come up with these bitwise arithmetic tricks on their own.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.