FROM:http://www.nowcoder.com/books/coding-interviews/4060ac7e3e404ad1a894ef3e17650423
实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
1 class Solution { 2 public: 3 string replaceSpace(string str) { 4 string rep = "%20"; 5 int pos = 0; 6 while(pos<str.size()) 7 { 8 if(str[pos]==' ') 9 { 10 str.replace(pos,1,rep); 11 pos+=2; 12 }else 13 { 14 pos+=1; 15 } 16 } 17 return str; 18 } 19 };