Skip to main content
added code
Source Link
Damian
  • 4.7k
  • 6
  • 44
  • 71

If you want to do it quickly, you can use a two scan approach. Pseudo code:

  1. Firstfirst parse. Findfind how many matching characterschars.
  2. Expandexpand the length of the string.
  3. Secondsecond parse. Start from the end of the string. When when we get a match we replace, else we just copy the chars from the first string.

I am not sure if this can be optimized to an in-place algorithmalgo.

And a C++11 code example but I only search for one char.

#include <string> #include <iostream> #include <algorithm> using namespace std; void ReplaceString(string& subject, char search, const string& replace) { size_t initSize = subject.size(); int count = 0; for (auto c : subject) { if (c == search) ++count; } size_t idx = subject.size()-1 + count * replace.size()-1; subject.resize(idx + 1, '\0'); string reverseReplace{ replace }; reverse(reverseReplace.begin(), reverseReplace.end()); char *end_ptr = &subject[initSize - 1]; while (end_ptr >= &subject[0]) { if (*end_ptr == search) { for (auto c : reverseReplace) { subject[idx - 1] = c; --idx; } } else { subject[idx - 1] = *end_ptr; --idx; } --end_ptr; } } int main() { string s{ "Mr John Smith" }; ReplaceString(s, ' ', "%20"); cout << s << "\n"; } 

If you want to do it quickly, you can use a two scan approach. Pseudo code:

  1. First parse. Find how many matching characters.
  2. Expand the length of the string.
  3. Second parse. Start from the end of the string. When we get a match we replace, else we just copy the chars from the first string.

I am not sure if this can be optimized to an in-place algorithm.

If you want to do it quickly you can use a two scan approach. Pseudo code:

  1. first parse. find how many matching chars.
  2. expand the length of the string.
  3. second parse. Start from the end of the string when we get a match we replace, else we just copy the chars from the first string.

I am not sure if this can be optimized to an in-place algo.

And a C++11 code example but I only search for one char.

#include <string> #include <iostream> #include <algorithm> using namespace std; void ReplaceString(string& subject, char search, const string& replace) { size_t initSize = subject.size(); int count = 0; for (auto c : subject) { if (c == search) ++count; } size_t idx = subject.size()-1 + count * replace.size()-1; subject.resize(idx + 1, '\0'); string reverseReplace{ replace }; reverse(reverseReplace.begin(), reverseReplace.end()); char *end_ptr = &subject[initSize - 1]; while (end_ptr >= &subject[0]) { if (*end_ptr == search) { for (auto c : reverseReplace) { subject[idx - 1] = c; --idx; } } else { subject[idx - 1] = *end_ptr; --idx; } --end_ptr; } } int main() { string s{ "Mr John Smith" }; ReplaceString(s, ' ', "%20"); cout << s << "\n"; } 
Copy edited.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

If you want to do it quickly, you can use a two scan approach. Pseudo code:

  1. firstFirst parse. findFind how many matching charscharacters.
  2. expandExpand the length of the string.
  3. secondSecond parse. Start from the end of the string when. When we get a match we replace, else we just copy the chars from the first string.

I am not sure if this can be optimized to an in-place algoalgorithm.

If you want to do it quickly you can use a two scan approach. Pseudo code:

  1. first parse. find how many matching chars.
  2. expand the length of the string.
  3. second parse. Start from the end of the string when we get a match we replace, else we just copy the chars from the first string.

I am not sure if this can be optimized to an in-place algo.

If you want to do it quickly, you can use a two scan approach. Pseudo code:

  1. First parse. Find how many matching characters.
  2. Expand the length of the string.
  3. Second parse. Start from the end of the string. When we get a match we replace, else we just copy the chars from the first string.

I am not sure if this can be optimized to an in-place algorithm.

Source Link
Damian
  • 4.7k
  • 6
  • 44
  • 71

If you want to do it quickly you can use a two scan approach. Pseudo code:

  1. first parse. find how many matching chars.
  2. expand the length of the string.
  3. second parse. Start from the end of the string when we get a match we replace, else we just copy the chars from the first string.

I am not sure if this can be optimized to an in-place algo.