Based on char*
Char*replace (CHAR*SRC, Char*sub, CHAR*DST)
{
int pos = 0;
int offset = 0;
int Srclen, Sublen, Dstlen;
Char*pret = NULL;
Srclen = strlen (src);
Sublen = strlen (sub);
Dstlen = strlen (DST);
PRet = (char*) malloc (Srclen + Dstlen-sublen + 1);//(external whether that space) if (NULL!= pRet)
{
pos = STRSTR (src, sub)-src;
memcpy (PRet, SRC, POS);
Offset + + pos;
memcpy (PRet + offset, DST, Dstlen);
Offset + + Dstlen;
memcpy (pRet + offset, src + pos + sublen, Srclen-pos-sublen);
Offset + + Srclen-pos-sublen;
* (PRet + offset) = ' ";
}
return pRet;
}
Description
parameters, src string source, string to be replaced by the sub, DST, string to replace
Char*replace (CHAR*SRC, Char*sub, CHAR*DST)
{
Record current pointer position
int pos = 0;
Record offset
int offset = 0;
String length
int Srclen, Sublen, Dstlen;
Back to Content
Char*pret = NULL;
The length of each string is evaluated
Srclen = strlen (src);
Sublen = strlen (sub);
Dstlen = strlen (DST);
Request a replacement string buffer. Replace sub with DST, so it should be srclen-sublen+dstlen,+1 out of ' a ' location
PRet = (char*) malloc (Srclen + Dstlen-sublen + 1);//(external whether that space) if (NULL!= pRet)
{
Strstr to find the pointer that appears in a sub string. The pointer subtracts the SRC address. Get a relative position
pos = STRSTR (src, sub)-src;
Copy src string, starting from the first address, POS characters.
memcpy (PRet, SRC, POS);
Increase offset to Pos
Offset + + pos;
Copy DST to the return content.
memcpy (PRet + offset, DST, Dstlen);
Relocate offset
Offset + + Dstlen;
In copy src, the string following the sub string into the Pret
memcpy (pRet + offset, src + pos + sublen, Srclen-pos-sublen);
Relocate offset
Offset + + Srclen-pos-sublen;
Finally, add the end of the string to mark ' the '
* (PRet + offset) = ' ";
}
Returns the newly constructed string
return pRet;
}
C + +-based string class
inline void Csreplace (std::string& s1,std::string& s2,std::string& s3,int count) {Std::string::size_type pos=0; Std::string::