// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>

using namespace std;

void RemoveWS1(string& s)
{
    for(string::size_type ix = 0; ix < s.size(); ix++)
    {
        if(s[ix] == ' ')
        {
            s.erase(ix,1);
        }
    }
}

void RemoveWS2(string& s)
{
    string::size_type ix = 0;
    while((ix = s.find(' ',ix)) != string::npos)
    {
        s.erase(ix,1);
    }
}

void RemoveWS3(string& s)
{
    string::size_type ix2 = 0;
    for(string::size_type ix = 0; ix < s.size(); ix++)
    {
        if(s[ix] != ' ')
        {
            s[ix2] = s[ix];
            ix2++;
        }
    }
    s.erase(ix2);
}

void RemoveWS4(string& s)
{
    string::size_type ix = s.find(' ',0);
    string::size_type ix2 = ix;
    while(ix != string::npos)
    {
        string::size_type tmp = s.find(' ',ix+1);
        int len = ((tmp != string::npos) ? tmp : s.size()) - ix - 1;
        //s.replace(ix2,len,s,ix+1,len);
        for(string::size_type i = ix2; i < ix2+len; i++)
        {
            s[i] = s[i-ix2+ix+1];
        }
        ix = tmp;
        ix2 += len;
    }
    if(ix2 != string::npos)
    {
        s.erase(ix2);
    }
}

void RemoveWS5(string& s)
{
    s.erase(remove(s.begin(), s.end(), ' '), s.end()); 
}

const int N = 1000000;
const string MASTER = " a b c d e f g h i ";
const string NONE = "abcdefghi";
const string ONE = "xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxx";

void FuncTest()
{
    string s;
    s = MASTER;
    RemoveWS1(s);
    cout << "|" << s << "|" << endl;
    s = MASTER;
    RemoveWS2(s);
    cout << "|" << s << "|" << endl;
    s = MASTER;
    RemoveWS3(s);
    cout << "|" << s << "|" << endl;
    s = MASTER;
    RemoveWS4(s);
    cout << "|" << s << "|" << endl;
    s = MASTER;
    RemoveWS5(s);
    cout << "|" << s << "|" << endl;
    s = NONE;
    RemoveWS1(s);
    cout << "|" << s << "|" << endl;
    s = NONE;
    RemoveWS2(s);
    cout << "|" << s << "|" << endl;
    s = NONE;
    RemoveWS3(s);
    cout << "|" << s << "|" << endl;
    s = NONE;
    RemoveWS4(s);
    cout << "|" << s << "|" << endl;
    s = NONE;
    RemoveWS5(s);
    cout << "|" << s << "|" << endl;
    s = ONE;
    RemoveWS1(s);
    cout << "|" << s << "|" << endl;
    s = ONE;
    RemoveWS2(s);
    cout << "|" << s << "|" << endl;
    s = ONE;
    RemoveWS3(s);
    cout << "|" << s << "|" << endl;
    s = ONE;
    RemoveWS4(s);
    cout << "|" << s << "|" << endl;
    s = ONE;
    RemoveWS5(s);
    cout << "|" << s << "|" << endl;
}

void PerfTest(string label, string original)
{
    cout << label << ":" << endl;
    clock_t t1 = clock();
    for(int i = 0; i < N; i++)
    {
        string s = original;
        RemoveWS1(s);
    }
    clock_t t2 = clock();
    cout << "RemoveWS1 = " << (t2-t1) << endl;
    clock_t t3 = clock();
    for(int i = 0; i < N; i++)
    {
        string s = original;
        RemoveWS2(s);
    }
    clock_t t4 = clock();
    cout << "RemoveWS2 = " << (t4-t3) << endl;
    clock_t t5 = clock();
    for(int i = 0; i < N; i++)
    {
        string s = original;
        RemoveWS3(s);
    }
    clock_t t6 = clock();
    cout << "RemoveWS3 = " << (t6-t5) << endl;
    clock_t t7 = clock();
    for(int i = 0; i < N; i++)
    {
        string s = original;
        RemoveWS4(s);
    }
    clock_t t8 = clock();
    cout << "RemoveWS4 = " << (t8-t7) << endl;
    clock_t t9 = clock();
    for(int i = 0; i < N; i++)
    {
        string s = original;
        RemoveWS5(s);
    }
    clock_t t10 = clock();
    cout << "RemoveWS5 = " << (t10-t9) << endl;
}

int main()
{
    FuncTest();
    PerfTest("Short with spaces", MASTER);
    PerfTest("Short without spaces", NONE);
    PerfTest("Long with spaces", MASTER + MASTER + MASTER + MASTER + MASTER + MASTER + MASTER + MASTER + MASTER + MASTER);
    PerfTest("Long without spaces", NONE + NONE + NONE + NONE + NONE + NONE + NONE + NONE + NONE + NONE);
    PerfTest("One space in middle", ONE);
    return 0;
}
