rootUser

Replace space with 20 in a string

May 27th, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* C programme to replace a strings spaces with %20 */
  2. #include <stdio.h>
  3. int main(void)
  4. {
  5.     int i,j=0;
  6.     char in[1000],out[1000];
  7.     gets(in);
  8.     for (i=0;in[i]!='\0';i++)
  9.         {
  10.         if (in[i] == ' ')
  11.             {
  12.             out[j++]='%';
  13.             out[j++]='2';
  14.             out[j++]='0';
  15.         }
  16.         /*
  17.         WE CAN ALSO WRITE IT LIKE THAT
  18.                if(in[i]==' ')
  19.             {
  20.                     out[j] = '%';
  21.             out[j+1] = '2';
  22.             out[j+2] = '0';
  23.             j=j+3;
  24.             }
  25.         */
  26.         else
  27.         {
  28.             out[j]=in[i]; //it is also correct out[j++]=in[i]; then we need not to write j++; later
  29.             j++;
  30.         }
  31.     }
  32.     out[j]='\0';
  33.     puts(out);
  34.     return 0;
  35. }
Add Comment
Please, Sign In to add comment