The sword refers to Offer_Programming questions_2

Topic description

Please implement a function to replace spaces in a string with "%20". For example, when the string is We Are Happy., the replaced string is We%20Are%20Happy.
 
class Solution {
public:
	void replaceSpace(char *str,int length) {
        int oldStringLength;
        int numBlank = 0;
        for(oldStringLength = 0; str[oldStringLength]!='\0';oldStringLength++) {
            if (str[oldStringLength] == ' ') {
                numBlank++;
            }
        }
        int curStringLength = oldStringLength + numBlank * 2;
        if (curStringLength > length) return;
        str[curStringLength] = '\0';
        for(int j = oldStringLength-1,i = curStringLength-1; j != i; j--) {
            if (str[j] == ' ') {
                str [i] = '0';
                str [i-1] = '2';
                str [i-2] = '%';
                i = i-3;
            } else {
                str[i] = str[j];
                i--;
            }
        }
	}
};

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324693284&siteId=291194637