English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

2 answers

the most efficient way would be to have 2 pointers, one at the first character and the other at the last element. Then you swap the elements pointed to by the pointers. then you increment the value of the first pointer and decrement the value of the second pointer and then swap the elements again.
you continue this process until both your pointers meet or cross each other, at which point your string is not completely swapped.

Hope that helps.

2006-08-25 07:07:29 · answer #1 · answered by americanDesi 2 · 1 0

Hi,

First thank you for asking this question. I found it to be a very good exercise to think through the various possibilities. I haven't done a problem like this since my grad school days at Harvard. :)

My answer would be to start with the last character and loop backwards.

In each iteration of the loop, merely assign the next pointer of the current character to its previous.

After the loop set the previous of the original last character to null.

I believe this is the most efficient method as it requires no additional pointers, 1 iteration through the loop, and only N+1 operations where N is the number of letters in the string.

Just my two cents,

Leo

2006-08-26 17:55:41 · answer #2 · answered by Leo R 2 · 0 0

fedest.com, questions and answers