Advertisement
arif334

C++ String-Replace-All

Jan 10th, 2023 (edited)
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | Source Code | 0 0
  1. // A helper function to replace "all" occurances of "src" with "dest" in a C++ string. (collected)
  2. string replaceAll(string str, const string& src, const string& dest) {
  3.     auto start_pos = 0;
  4.     while((start_pos = str.find(src, start_pos)) != string::npos) {
  5.         str.replace(start_pos, src.length(), dest);
  6.         start_pos += dest.length(); // Handles case where 'dest' is a substring of 'src'
  7.     }
  8.     return str;
  9. }
  10.  
  11. // Sample Problem: LeetCode 1108
Tags: C++ String
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement