Skip to content

Instantly share code, notes, and snippets.

@iwilbert
Created June 17, 2014 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwilbert/d0efe909f1311c081bc2 to your computer and use it in GitHub Desktop.
Save iwilbert/d0efe909f1311c081bc2 to your computer and use it in GitHub Desktop.
public void replace(char[] line) {
int len; // original length;
int cnt = 0; // space count
for(len = 0; line[len] != '\0'; len++)
{
if(line[len] == ' ')
cnt++;
}
int new_len = len + cnt*2; // length of new string
int k = new_len - 1; // starts from the end of new string
for(int i = len - 1; i >= 0; i--) // i starts from the end of original string
{
if(line[i] == ' ')
{
line[k] = '0';
line[k-1] = '2';
line[k-2] = '%';
k -= 2;
}
else
line[k] = line[i];
k--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment