class Solution { public: void replaceSpace(char* str, int length) { char* str_new = str; int num = 0; for (int i = 0; i < length; i++) { if (str[i] == ' ') { num++; } } char* pStr = (char*)malloc(sizeof(char) * length + num*2); char* pStr_new = pStr; for (int i = 0; i < length; i++) { if (str[i] == ' ') { *(pStr_new++) = '%'; *(pStr_new++) = '2'; *(pStr_new++) = '0'; str_new++; } else { *pStr_new++ = *str_new++; } } strcpy(str, pStr); } };