C Program for replacing space with %20

Program for replacing space with %20

#include < stdio.h >
#include < string.h >
int main()
{
char str[100];
printf("Enter The String: ");
gets(str);
int i,new;
int space=0;
for(i=0;i < strlen(str);i++)
{
if(str[i]==' ')
{
space++;
}
}
new=strlen(str)+2*space;
str[new]='\0';
for(i=strlen(str)-1;i > =0;i--)
{
if(str[i]==' ')
{
str[new-1]='0';
str[new-2]='2';
str[new-3]='%';
new=new-3;
}
else
{
str[new-1]=str[i];
new=new-1;
}
}
printf("Spaces After Replace: %s\n",str);
}

Image result for c code

No comments