Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7741457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:02:14+00:00 2026-06-01T09:02:14+00:00

i want to replace a character in the string with a string. can i

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:02:15+00:00Added an answer on June 1, 2026 at 9:02 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to replace last input character from keyboard to '' My String Input
I can use this: String str = TextX Xto modifyX; str = str.replace('X','');//that does
I have a string that has a format like this: <b>*GTPersonnel</b><table border=1><tr><td>&#115;&#115;&#50;&#49;&#49;&#49;</td></tr></table> I want
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
I want to replace a charecter in a string with a string in c#.
I want to replace with the 4~8 characters of a string with * ,how
I want to remove characters in a string in python: string.replace(',', '').replace(!, '').replace(:, '').replace(;,
I want to replace and insert escape character sequences with their escape character values,
I want to replace the last String which is a , with ) .
How can I replace this character | in JavaScript? <html> <body> <script type=text/javascript> var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.