This is an interview question Looking for best optimal solution to trim multiple spaces from a string. This operation should be in-place operation.
input =
Functional variant in Haskell:
import Data.List (intercalate)
trimSpaces :: String -> String
trimSpaces = intercalate " " . words
The algorithm the next: