题目描述

请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

​https://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=11155&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking​

题解:

好久没用char*不适应了。。

class Solution {
public:
void replaceSpace(char *str,int length) {
for (int i = 0; i < length; i++) {
if (str[i] == ' ') {
length += 2;
for (int j = length; j > i + 2; j--) {
str[j] = str[j - 2];
}
str[i] = '%';
str[i + 1] = '2';
str[i + 2] = '0';
}
}
}
};