Take the 2-minute tour ×

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

share|improve this question
Where do you expect the extra characters will go? –  AShelly Apr 5 '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 5 '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 5 '12 at 14:44
void main() is illegal. Please change to int main(). –  ipc Apr 5 '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 5 '12 at 14:55
add comment

2 Answers

up vote 1 down vote accepted

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.

Edit: though it's not a concern with the strings you used, as @ipc pointed out, 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 (int pos=0; 
    x.replace(pos, old.length(), rep);
share|improve this answer
If the replacement string contains an 'o', this will an endless loop. –  ipc Apr 5 '12 at 15:16
@ipc: good point. I've added some commentary/code for dealing with that. –  Jerry Coffin Apr 5 '12 at 15:25
Thats a great test case... –  Madu Apr 5 '12 at 15:55
add comment

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.

share|improve this answer
add comment

Your Answer


