Programming question:

Reverse words in a string (words are separated by one or more spaces). Now do it in-place.

What does "in-place" mean in the above context for an interpreted language like PHP or JavaScript?

  • 3
    Possible duplicate in SO : stackoverflow.com/questions/2779797/what-does-in-place-mean
    – AllTooSir
    Apr 17, 2013 at 8:55
  • 1
    Possible? The "Reverse words..." part is duplicated word-for-word.
    – user8709
    Apr 17, 2013 at 9:06
  • I apologize, I hadn't seen the dupe question. Feel free to close, merge or move this one. Apr 17, 2013 at 13:47
  • I went ahead and edited it to be more specific and remove duplication. Apr 17, 2013 at 13:49
  • 1
    @LeviHackwith: I really don't think that that context really changes anything. It's still the same question and the same answers still apply. Apr 17, 2013 at 14:20

4 Answers 4


It depends on how low-level the language you're working with is, but assuming something like C it means "do not take the string apart and put it back together in a different part of memory, modify the part of memory the string is already in".

I.e. not:

1. foo bar baz
2. foo bar baz -> baz
3. foo bar baz -> baz bar
4. foo bar baz -> baz bar foo


1. foo bar baz
2. baz foo bar
3. baz bar foo

(Or however exactly your algorithm will work.)


It normally means to perform the operation without using any additional memory. In terms of space complexity, the auxiliary space complexity would be O(1).

In practice, depending on implementation, temporary variables may be allowed. In this case the output should use same location in memory as the input.


In this context, it refers to modifying the original variable, as opposed to using further memory. This means that, as opposed to running through the original string and copying each character to a new string variable, you need to swap pieces of the original as you are viewing it.

Wikipedia actually has an entire article on the topic, citing the topic's relevance and usefulness relating to minimizing memory usage.


In place will be something of this sort :

void reverse(char *str)
   reverse(strtok(NULL," "));
   printf("%s ",revStr);

"An in-place algorithm (or in Latin in situ) is an algorithm which transforms input using a data structure with a small, constant amount of extra storage space. The input is usually overwritten by the output as the algorithm executes."
Source: In-place algorithm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.