c++ 双指针

class Solution {
public:
    void replaceSpace(char* str, int length) {
        if(str == NULL || length <0)
            return;
        int num =0;    //空格数目
        for (int i = 0; i < length; i++){
            if (str[i] == ' ')
                num++;
        }
        char* p1 = &str[length -1];//原字符串指针
        char* p2 = &str[length -1 +num*2];//新字符串指针
        for (int i = length-1; i >= 0; i--) {
            if (str[i] == ' ') {
                *p2-- = '0';
                *p2-- = '2';
                *p2-- = '%';
                p1--;
            }else{
                *p2-- = *p1--;
            }
        }
    }
};