# Space Replacement

## Question

### Problem Statement

Write a method to replace all spaces in a string with %20. The string is given in a characters array, you can assume it has enough space for replacement and you are given the true length of the string.

You code should also return the new length of the string after replacement.

##### Notice

If you are using Java or Python，please use characters array instead of string.

Example

Given "Mr John Smith", length = 13.

The string after replacement should be "Mr%20John%20Smith", you need to change the string in-place and return the new length 17.

Challenge

Do it in-place.

## 题解

### C++

int replaceBlank(char string[], int length) {
int n = 0;
for (int i=0; i<length; i++)
if (string[i] == ' ') n++;

int new_len = length + n*2;
for (int i=length-1; i>=0; i--) {
if (string[i] != ' ') {
string[--new_len] = string[i];
} else {
string[--new_len] = '0';
string[--new_len] = '2';
string[--new_len] = '%';
}
}
return length + n*2;
}


### Java

public class Solution {
/**
* @param string: An array of Char
* @param length: The true length of the string
* @return: The true length of new string
*/
public int replaceBlank(char[] string, int length) {
if (string == null) return 0;

int space_cnt = 0;
for (int i = 0; i < length; i++) {
if (string[i] == ' ') {
space_cnt++;
}
}
final int new_length = 2*space_cnt + length;

int right = new_length - 1;
for (int i = length - 1; i >= 0; i--) {
if (string[i] == ' ') {
string[right--] = '0';
string[right--] = '2';
string[right--] = '%';
} else {
string[right--] = string[i];
}
}

return new_length;
}
}