-
Code Crash!!!
Hi,
I've written a code which replaces characters in a string with their respective URLEncode (i.e a " " will be replaced by "+" etc..).
However the code crashes at execution.
Please help!!
Here's the code extract;
Code:
--------------------------------------------------------------------------------
for(j=0;j<one.length();j++)
{
indxD = one.find_first_of ( " " , 0 );
one.replace(indxD,1,"%2B",3);
}
---------------------------------------------------------------------------------
Thank You In Anticipation,
John
-
Why this was doing on the contest board is beyond me!
MOVED!
-
uhhmm...maybe you should paste some more code?
It's kinda hard to tell what goes wrong from what you've shown us...
/btq :)
-
To have a stab at it;
Code:
#include <iostream>
#include <string>
int main(){
using std::cout;
using std::endl;
using std::string;
string one("Hello World This Is Encoded");//string to encode
const string rep("%2B");//replacement token
string::size_type indxD = 0;//return of find...
std::cout << "Unencoded " << one << std::endl;
while(1){
indxD = one.find_first_of (' ',0);//find a char....
if(indxD == string::npos)break;//if -1 then end (not found!)
else one.replace(indxD,1,rep.c_str(),rep.length());//replace with token
}
std::cout << "Encoded " << one << std::endl;
}
-
I don't think there is something wrong with the loop, maybe it crashes from something else.