alg: replace the substring
#include <string>
#include <iostream>
//replace the substring l to r for source string s, and put result in d
void replace(const std::string& s,const std::string& l,const std::string& r,std::string& d)
{
size_t i=0, n=s.length(), nl=l.length();
while( 1 )
{
size_t pos = s.find_first_of(l,i);
if(std::string::npos==pos)
{
d += s.substr(i);
break;
}
else
{
if(pos-i>0)
d += s.substr(i,pos-i);
d += r;
i = pos + nl;
}
}
}
void main()
{
std::string d;
replace("ddwv", "def", "_", d);
std::cout<<d;
}
#include <iostream>
//replace the substring l to r for source string s, and put result in d
void replace(const std::string& s,const std::string& l,const std::string& r,std::string& d)
{
size_t i=0, n=s.length(), nl=l.length();
while( 1 )
{
size_t pos = s.find_first_of(l,i);
if(std::string::npos==pos)
{
d += s.substr(i);
break;
}
else
{
if(pos-i>0)
d += s.substr(i,pos-i);
d += r;
i = pos + nl;
}
}
}
void main()
{
std::string d;
replace("ddwv", "def", "_", d);
std::cout<<d;
}