Thursday, October 24, 2013

[Inmobi Interview] String: Replace space with %20

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

1 comment:

  1. #include
    #include

    void replaceFunc(char str[],int length)
    {
    int spaceCount=0,newLength,i=0;
    for(i=0;str[i];i++)
    if(str[i]==' ')
    spaceCount++;

    newLength=length+spaceCount*2;
    str[newLength]='\0';
    for(i=length-1;i>=0;i--)
    {
    if(str[i]==' ')
    {
    str[newLength-1]='0';
    str[newLength-2]='2';
    str[newLength-3]='%';
    newLength=newLength-3;
    }
    else
    {
    str[newLength-1]=str[i];
    newLength--;
    }
    }

    }

    int main(void) {
    char str[]="the dog";
    replaceFunc(str,strlen(str));
    printf("%s",str);
    return 0;
    }

    ReplyDelete