convert spaces to %20

Given a URL, write a function that converts all the spaces to %20.

Comments

  1. Give a pointer of the new string :

    char *urlspace_alloc(char *str)
    {
    char *ptr,*p;
    int x,i; for(x=i=0;str[i];i++) if(str[i]==' ') x++;
    ptr=malloc(sizeof(char)*(i+(x*3)+1));
    if(ptr==NULL) return NULL;
    else{
    for(x=i=0;str[i];i++){
    if(str[i]==' '){
    p=&ptr[x];
    memcpy(p,"%20",3);
    x+=3;
    } else {
    ptr[x]=str[i];
    x++;
    }
    }
    ptr[x]='\0';
    }
    return ptr;
    }

    ReplyDelete

Post a Comment

Popular posts from this blog