- #include
- using namespace std;
- class Solution {
- public:
- voidreplaceSpace(char*str,int length) {
- if(str == NULL || length<=0)
- return;
- intlength_origin =0;
- intblank =0;
- while(*str !='\0'){
- if(*str ==' '){
- blank++;
- length_origin++;
- str++;
- }
- else{
- length_origin++;
- str++;
- continue;
- }
- }
- cout<<'a'<<endl;
- intlength_new = length_origin+2*blank;
- cout<<'a'<<endl;
- if(length_new > length)
- return;
- else{cout<<'b'<<endl;
- char*str_new = str+2*blank;
- cout<<'b'<<endl;
- while(str_new != str){
- if(*str ==' '){
- *str_new ='0';
- *(str_new-1) ='2';
- *(str_new-2) ='%';
- str_new = str_new -3;
- str--;
- }
- else{
- *str_new =*str;
- str_new--;
- str--;
- }
- }
- }
- }
- };
- int main(){
- // cout<<'a';
- charstr[] ="we are happy";
- cout<endl;
- Solution replace;
- replace.replaceSpace(str,50);
- cout<<'a';
- cout<<'a'<<endl;
- cout<<'a'<<endl;
- cout<endl;
- return 0;
- }
来源: http://www.bubuko.com/infodetail-1989402.html