Count the number of spaces during the first scan of the string.
Parse the string again from the end and for each character:
If a space is encountered, store “%20”.
Else, store the character as it is in the newly shifted location.
voidreplaceSpaces() {
char src[] ="helo b";
int len =0, spaces =0;
/* Scan through src counting spaces and length at the same time */while (src[len]) {
if (src[len] ==' ')
++spaces;
++len;
}
/* Figure out how much space the new string needs (including 0-term) and allocate it */int newLen = len + spaces *2+1;
char* dst =malloc(newLen);
/* Scan through src and either copy chars or insert %20 in dst */int srcIndex =0, dstIndex =0;
while (src[srcIndex]) {
if (src[srcIndex] ==' ') {
dst[dstIndex++] ='%';
dst[dstIndex++] ='2';
dst[dstIndex++] ='0';
++srcIndex;
} else {
dst[dstIndex++] = src[srcIndex++];
}
}
dst[dstIndex] ='\0';
/* Print the result */printf("New string: '%s'\n", dst);
/* And clean up */free(dst);
return0;
}