Prog error: for replacing spaces with "%20"

Posted by assasinC on Stack Overflow See other posts from Stack Overflow or by assasinC
Published on 2012-07-08T03:02:51Z Indexed on 2012/07/08 3:15 UTC
Read the original article Hit count: 216

Filed under:

below is the prog i am compiling for replacing spaces with "%20" but when I run it output window shows blank and a message "arrays5.exe has occurred a prob"

#include <iostream>
#include<cstring>

using namespace std;

void method(char str[], int len)               //replaces spaces with "%20"
{

    int spaces, newlen,i;

    for (i=0;i<len;i++)
        if(str[i]==' ') spaces++;
    newlen=len+spaces*2;
    str[newlen]=0;
    for (i=len-1;i>=0;i--)
    {
        if(str[i]==' ')
        {
            str[newlen-1]='0';
            str[newlen-2]='2';
            str[newlen-3]='%';
            newlen=newlen-3;
        }
        else
        {
            str[newlen-1]=str[i];
            newlen=newlen-1;
        }
    }
}
int main()
{
    char str[20]="sa h ";
    method(str,5);
    cout <<str<<endl;
    return 0;
}

Please help me finding the error.Thanks

© Stack Overflow or respective owner

Related posts about c++