读书人

请问c语言中 字符串替换的有关问题

发布时间: 2012-04-09 13:41:25 作者: rapoo

请教c语言中 字符串替换的问题
将一个字符串中的 \ 替换成 \\


即 "aaa\\aaa"




"aaa\\\\aaa"


[解决办法]
如果可以用STL

C/C++ code
int string_replace(string &str, const string strsrc, const string strdst){           string::size_type pos=0;    string::size_type srclen=strsrc.size();    string::size_type dstlen=strdst.size();    int num = 0;    while( (pos=str.find(strsrc, pos)) != string::npos)    {               str.replace(pos, srclen, strdst);        pos += dstlen;        num++;    }               return num; } 

读书人网 >C语言

热点推荐