2、字符串中“%20”替换空格

题目描述

请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
思路:
1、从前往后插入太复杂
2、从后往前插入较简单
class Solution {
public:
    void replaceSpace(char *str,int length) {
        int i,j;
        i = length - 1;
        while(i >=0 )
        {
            if(str[i] == ' ')
            {
                for(j = length;j>=i;j--)
                {
                    str[j+2] = str[j];
                }
                str[i]='%';
                str[i+1]='2';
                str[i+2]='0';
                length+=2;
            }
            i--;
        }
    }
};
原文地址:https://www.cnblogs.com/zhuifeng-mayi/p/10738275.html