1 class Solution { 2 public: 3 /** 4 * @param string: An array of Char 5 * @param length: The true length of the string 6 * @return: The true length of new string 7 */ 8 int replaceBlank(char string[], int length) { 9 // Write your code here 10 if (!length) return 0; 11 int spaces = 0; 12 for (int i = 0; string[i]; i++) 13 if (string[i] == ' ') 14 spaces++; 15 if (!spaces) return length; 16 int newlen = length + spaces * 2; 17 int pn = newlen - 1, p = length - 1; 18 while (p >= 0 && pn >= 0) { 19 if (string[p] == ' ') { 20 string[pn--] = '0'; 21 string[pn--] = '2'; 22 string[pn--] = '%'; 23 p--; 24 } 25 else string[pn--] = string[p--]; 26 } 27 return newlen; 28 } 29 };