【思路】一旦找到空格则将空格后的字符串整体后移两位,然后将%20插入空格处
1 //length为牛客系统规定字符串输出的最大长度,固定为一个常数 2 class Solution { 3 public: 4 void replaceSpace(char *str,int length) { 5 int i,j; 6 for(i = 0; i < length; i ++) { 7 if(str[i] == ‘ ‘) { 8 for(j = length; j >= i + 3; j --) { 9 str[j] = str[j-2]; 10 } 11 str[i] = ‘%‘; 12 str[i+1] = ‘2‘; 13 str[i+2] = ‘0‘; 14 } 15 } 16 } 17 };
原文:http://www.cnblogs.com/lca1826/p/6436273.html