How to replace the last occurrence of a substring in a string with C++

1 Answer

0 votes
#include <iostream>
 
int main() {
    std::string s = "c++ c php nodejs java golang nodejs";
    std::string subs = "node";
    
    std::size_t pos = s.rfind(subs);

    if (pos != std::string::npos)
        s.replace(pos, subs.length(), "express");
        
    std::cout << s;
        
}
 
 
 
/*
run:
 
c++ c php nodejs java golang expressjs
 
*/

 



answered Feb 20, 2020 by avibootz
edited Feb 20, 2020 by avibootz
...