C programme to replace a strings spaces with %20

How to write a C programme to replace a strings spaces with %20 ?


Solution:
/* C programme to replace a strings spaces with %20 */
#include <stdio.h>
int main(void)
{
int i,j=0;
char in[1000],out[1000];
gets(in);
for (i=0;in[i]!='\0';i++)
    {
if (in[i] == ' ')
        {
out[j++]='%';
out[j++]='2';
out[j++]='0';
}
/*
WE CAN ALSO WRITE IT LIKE THAT
      if(in[i]==' ')
        {
            out[j] = '%';
out[j+1] = '2';
out[j+2] = '0';
j=j+3;
        }
*/
else
{
out[j]=in[i]; //it is also correct out[j++]=in[i]; then we need not to write j++; later
j++;
}
}
out[j]='\0';
puts(out);
return 0;
}

This method to replace all spaces in a string with '%20'.


Learn More :