Just FYI, I tried programming three different methods and timed each of them.
Method 1: Replace 2 spaces by 1 until no more double-spaces.
Method 2: Scan string and move each character downwards except if there are multiple spaces.
Method 3: Replace 2^x+1 spaces with 1 space, decreasing x until you're done.
Results varied widely between the three methods, each having cases where it did better or worse than the others.
Method 1 did best overall, with its strength being for long strings with short lengths of white space in them.
Method 2 did best on shorter strings.
Method 3 vastly outperformed the others on longer strings that were nearly all spaces.
Keep Up with TechRepublic