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

  • SEARCH
  • Home
  • 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 746265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:04:16+00:00 2026-05-14T14:04:16+00:00

This particular assignment has to do with removing substrings from strings; I am trying

  • 0

This particular assignment has to do with removing substrings from strings; I am trying some of the Stanford SEE courses online to learn some new languages.

What I’ve got so far is below, but if text = "hello hello" and remove ="el", it gets stuck in a loop, but if i change text to text = "hello hllo", it works, making me think I’m doing something obviously stupid.

There is a stipulation in the assignment not to modify the incoming strings, and instead to return a new string.

string CensorString1(string text, string remove){
    string returned;
    size_t found=0, lastfound=0;
    found = (text.substr(lastfound,text.size())).find(remove);
    while (string::npos != found ){
        returned += text.substr(lastfound,found);
        lastfound = found + remove.size();
        found = (text.substr(lastfound,text.size())).find(remove);
    }
    returned += text.substr(lastfound,found);
    return returned;
}

Guidance would be appreciated 🙂 Thanks

UPDATE

Took the very kind advice given and modified my code to this :

string CensorString1(string text, string remove){
string returned;
size_t found=0, lastfound=0;
found = text.find(remove);
while (string::npos != found ){
    returned += text.substr(lastfound,found);
    lastfound = found + remove.length();
    found = text.find(remove,lastfound);
}
returned += text.substr(lastfound);
return returned;
}

But still behaves the same

Any more ideas folks?

  • 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-05-14T14:04:16+00:00Added an answer on May 14, 2026 at 2:04 pm

    found = (text.substr(lastfound,text.size())).find(remove); is incorrect. It returns the index of the searched string in text.substr(lastfound,text.size()), but not in text.

    You should perhaps change this to found = text.find(text, lastfound);.

    Besides of being incorrect, taking a substring (this means, allocating a new string) and calculating an index in it is quite inefficient, unless the optimizer is super-smart.

    Moreover, the final returned += text.substr(lastfound,found); is incorrect too: you need to add the last chunk of the text, not the one till found index (which is most likely empty, as lastfound can be smaller than found. Better would be returned += text.substr(lastfound);

    Edit:
    In the second example, you need to replace returned += text.substr(lastfound,found);
    with returned += text.substr(lastfound,found-lastfound);. The second argument to substr is length, not position.

    With this change, the test example runs fine in my test program.

    (Addition by J.F. Sebastian:)

    string CensorString1(string const& text, string const& remove){
      string returned;
      size_t found = string::npos, lastfound = 0;
      do {
        found = text.find(remove, lastfound);
        returned += text.substr(lastfound, found-lastfound);
        lastfound = found + remove.size();
      } while(found != string::npos);
      return returned;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this particular case I'm trying to discover if a mylib.a file is 32
I seem to be getting this particular always when i am trying to receive
Classes confuse me! For this particular assignment, we are to do the classic 'Cards
This particular example relates to Django in Python, but should apply to any language
In this particular situation, there are 9 automated steps in a process that take
In this particular code sample I want to reference the second overloaded method (int
I know this particular query works, as I tested it with unprepared, procedural methods.
There seem to many ways to skin this particular cat - but which is
I haven't found an answer to this particular question; perhaps there isn't one. But
Cascading deletes aren't setup on this particular database. Just wondering if there's a way

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.