3


i want to replace a character in the string with a string. can i do it in-place? As the new string has length greater than original string.Question is that can i do with using additional buffer? for example

void replaceChar(std::string &input, std::string replacementString, char charToReplace)
{
//some code here. No additional buffer
}

void main(){

  std::string input = "I am posting a comment on LinkedIn";
  std::string replacementString = "pppp";
  char charToReplace = 'o';
  replaceChar(input, replacementString, charToReplace);
}

I only want the strategy (algorithm). it would be good if algorithm will be designed keeping some language in mind that will not dynamically increase or decrease the string length once it was initilized like c++

Madu
  • 4,849
  • 9
  • 44
  • 78
  • 1
    Where do you expect the extra characters will go? – AShelly Apr 05 '12 at 14:37
  • Ya thats what actually i am asking that because i have seen a code implementation in java that calculated the new length of the string and starts placing characters from the end to the start. That's why i am wondering that whether java allows the this type of change in string length at run time... – Madu Apr 05 '12 at 14:44
  • There is a way to insert a string beginning a particular position, removing the character at that position only but this will increase the size of internal buffer. – hmjd Apr 05 '12 at 14:44
  • 2
    `void main()` is illegal. Please change to `int main()`. – ipc Apr 05 '12 at 14:47
  • Do you mean that you are looking for only one re-allocation? So that it doesn't make it bigger, replace the first, make it bigger again, replace the 2nd, etc? You want pre-calculation and then only one allocation, then replaces all instances? Or do you want it to overwrite so that it really never changes the length? Like it would become "I am pPPPPng a cPPPPnt PPPPinkedIn"? – Kevin Anderson Apr 05 '12 at 14:55
  • 1
    Possible duplicate of [Replace substring with another substring C++](https://stackoverflow.com/questions/4643512/replace-substring-with-another-substring-c) – phuclv Aug 03 '18 at 05:17

4 Answers4

7

std::string has a replace member, but it works in terms of numerical positions, rather than the previous content of the string. As such, you normally have to combine it with the find member in a loop, something like this:

std::string old("o");

int pos;

while ((pos = x.find(old)) != std::string::npos)
    x.replace(pos, old.length(), "pppp");

Personally, I'd rarely get concerned about how often the string gets resized, but if it's a major concern, you can use std::count to find the number of occurrences of the old string, multiply by the difference in size between the old and new strings, and use std::string::reserve() to reserve enough space. Note, however, that reserve was added in C++11 -- older implementations won't have it.

Alhough it's not a concern with the strings you used, this doesn't work correctly if the replacement string contains an instance of the value being replaced. If you need to deal with that, you'll need to supply the offset in the string at which to start each search:

int pos = 0;

while ((pos = x.find(old, pos)) != std::string::npos) {
    x.replace(pos, old.length(), rep);
    pos += rep.length();
}

Or, you might prefer a for loop in this case:

    std::string old("o");
    std::string rep("pop");

for (std::size_t pos=0; 
    (pos = x.find(old, pos)) != std::string::npos; 
    pos+=rep.length())
{
    x.replace(pos, old.length(), rep);
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

I think you misundertand C++ std::string. It can actually change the string length dynamically. In internally does heap allocations, and will grow the buffer if necessary.

Jesse Pepper
  • 3,225
  • 28
  • 48
1

Here is a code that minimises the number of assignments and allocations. It is based on the following answer to a similar question: https://stackoverflow.com/a/32322122/3903076

The cases where the replacement string has length 0 or 1 are handled separately. Else, the string has to grow.

If there is not enough capacity, then an external buffer will be necessary anyway, so we just do copy-replace and swap.

The interesting case is when the string already has enough capacity, so we can actually do a non-trivial in-place replacement. We do that with a reverse copy-replace, stopping when we do not need to replace anything else.

This can be seen in the last line of the function.

void replaceChar(std::string& input, const std::string& replacementString, char charToReplace)
{
  if (replacementString.empty()) {
    input.erase(std::remove(input.begin(), input.end(), charToReplace), input.end());
    return;
  }
  if (replacementString.size() == 1) {
    std::replace(input.begin(), input.end(), charToReplace, replacementString.front());
    return;
  }

  const auto first_instance = std::find(input.begin(), input.end(), charToReplace);
  auto count = std::count(first_instance, input.end(), charToReplace);
  const auto extra_size = count * (replacementString.size() - 1);
  const auto new_size = input.size() + extra_size;

  if (input.capacity() < new_size) {
    std::string aux;
    aux.reserve(new_size);
    replace_with_range_copy(input.cbegin(), input.cend(), std::back_inserter(aux), charToReplace, replacementString.cbegin(), replacementString.cend());
    input.swap(aux);
    return;
  }

  input.resize(new_size);

  const auto rlast = std::make_reverse_iterator(first_instance);
  const auto rfirst = input.rbegin();
  const auto old_rfirst = rfirst + extra_size;

  replace_with_range_copy(old_rfirst, rlast, rfirst, charToReplace, replacementString.crbegin(), replacementString.crend());
}

Here is an implementation of the replace_with_range_copy algorithm:

template <typename InputIt1, typename OutputIt, typename T, typename InputIt2>
OutputIt replace_with_range_copy(InputIt1 first, InputIt1 last, OutputIt d_first, const T& old_value, InputIt2 new_first, InputIt2 new_last)
{
  InputIt1 next;
  while (true) {
    if (first == last) return d_first;
    next = std::find(first, last, old_value);
    d_first = std::copy(first, next, d_first);
    if (next == last) return d_first;
    d_first = std::copy(new_first, new_last, d_first);
    first = std::next(next);
  }
}
Community
  • 1
  • 1
filipos
  • 645
  • 6
  • 12
0

i tried this old-fashioned stuff and i think it works. here it is. i am not sure that this would work on encodings other than ascii.

#include <string>
#include <cstring>

std::string
replace_char_with_string
    (const char *in_p,
     char  from_ch,
     const char *to_p)
{
    char output_c_str[strlen(in_p)*2+1], *out_p = output_c_str;
    int  to_len = strlen(to_p);

    while (*in_p)
    {
        if (*in_p == from_ch)
        {
            strcpy(out_p, to_p);
            out_p += to_len;
        }
        else
        {
            *out_p++ = *in_p;
        }

        ++in_p;
    }
    *out_p = '\0';

    std::string output(output_c_str);
    return output;
}

// example usage
std::string java_namespace_name = "com.foo.boo";
std::string cpp_namespace_name = replace_char_with_string(java_namespace_name.c_str()
ksridhar
  • 189
  • 2
  • 9