class Replacement { public: string replaceSpace(string iniString, int length) { // count zeros int last = length + 2 * count(iniString.begin(), iniString.begin()+length, ' '); iniString.resize(last); // reverse filling for (int i = length-1, j = last-1; i >= 0; --i) { if (iniString[i] != ' ') iniString[j--] = iniString[i]; else { iniString.replace(j-2, 3, "%20"); j -= 3; } } return iniString; } };