Try..
std::string s = " dhanesh is not great ";
string s_toreplace = "not";
string s_replace = "very";
std::string::size_type idx = -1;
while((idx = s.find(s_toreplace, idx+1)) != std::string::npos)
s.replace(idx, strlen(s_toreplace.c_str()), s_replace);
cout<<s<<std::endl;