
Desicription
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
class { public: void replaceSpace(char *str,int length) { int blankCount = 0; int fontIndex = 0; for(; str[fontIndex]; fontIndex++) { if(str[fontIndex] == ' ') { blankCount++; } } realloc(str, static_cast<size_t>(fontIndex + (blankCount << 1) + 1)); int tailIndex = fontIndex + (blankCount << 1); while(fontIndex >= 0) { if(str[fontIndex] != ' ') { str[tailIndex--] = str[fontIndex]; } else { str[tailIndex--] = '0'; str[tailIndex--] = '2'; str[tailIndex--] = '%'; } fontIndex--; } } };
|
近期评论