1 void ReplaceBlank(char str[], int length) // length >= the real length of string 2 { 3 if (str == nullptr || length <= 0) 4 { 5 return; 6 } 7 int origin_str_len = 0; 8 int new_str_len = 0; 9 int i = 0; 10 int count_space = 0; 11 while (str[i] != '\0') 12 { 13 if (str[i] == ' ') 14 ++count_space; 15 ++origin_str_len; 16 ++i; 17 18 } 19 new_str_len = origin_str_len + 2 * count_space; 20 if (new_str_len > length) 21 return; 22 23 while (origin_str_len >= 0 && new_str_len > origin_str_len) 24 { 25 if (str[origin_str_len] == ' ') 26 { 27 str[new_str_len--] = '0'; 28 str[new_str_len--] = '2'; 29 str[new_str_len--] = '%'; 30 31 } 32 else 33 { 34 str[new_str_len--] == str[origin_str_len]; 35 } 36 --origin_str_len; 37 } 38 39 }