voidReplacespace (Char*STR,intlength) { if(str==null| | length<=0) return; intoriginlen=0; intnewlen=0; intSpace=0; while(str[originlen]!=' /') { if(str[originlen]==' ') Space++; Originlen++; } Originlen++; Newlen=originlen+2*space; if(originlen==1|| space==0|| Newlen>length)return; Char*p1,*P2; P1=&str[originlen-1]; P2=&str[newlen-1]; for(intI=0; i<newlen;i++) { if(*p1!=' ') { *p2=*P1; P1--; P2--; } Else { (pp='0'; P2--; (pp='2'; P2--; (pp='%'; P2--; P1--; } } return; }
Simplify the code:
voidReplacespace (Char*STR,intlength) { if(str==null| | length<=0) return; intoriginlen=0; intnewlen=0; intSpace=0; while(str[originlen]!=' /') { if(str[originlen++]==' ') Space++; } Originlen++; Newlen=originlen+2*space; if(originlen==1|| space==0|| Newlen>length)return; intp1=originlen-1; intp2=newlen-1; for(intI=0; i<newlen;i++) { if(str[p1]!=' ') Str[p2--]=str[p1--]; Else{STR[P2--]='0'; STR[P2--]='2'; STR[P2--]='%'; P1--; } } return; }
A sword means an offer. Replace spaces in a string