Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,037 questions

40,788 answers

573 users

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

Learn & Practice SQL


113 views
asked Feb 20, 2020 by avibootz
edited Apr 26, 2020 by avibootz

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
...