#include <iostream>
using namespace std;
void replaceBlanks(char *string,int length){
if(string==NULL||length<0)
return;
int originalLength=0;
int numbersofBlanks=0;
int i=0;
while(string[i]!='\0'){
originalLength++;
if(string[i]==' ')
numbersofBlanks++;
++i;
}
int newLength=originalLength+numbersofBlanks*2;
if(newLength>length)
return;
int indexOriginal=originalLength;
int indexNew=newLength;
// cout <<numbersofBlanks << originalLength<<endl;
while(indexOriginal<indexNew&&indexOriginal>=0){
if(string[indexOriginal]==' '){
string[indexNew --] = '0';
string[indexNew --] = '2';
string[indexNew --] = '%';
}else
string[indexNew--]=string[indexOriginal];
indexOriginal --;
}
//cout << string ;
}
int main() {
cout << "test\n";
char string[100]="i love u , good good study , day day up";
cout << string << endl;
replaceBlanks(string,100);
cout << string << endl;
return 0;
}
测试的结果如下:
test
i love u , good good study , day day up
i%20love%20u%20,%20good%20good%20study%20,%20day%20day%20up