Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Changing a String

substr() returns a new string and leaves the original as it is. Three other methods change the string itself:

  • text.insert(index, part) inserts part before the character at index.
  • text.erase(index, count) removes count characters, starting at index. Without count, it removes everything from index to the end.
  • text.replace(index, count, part) removes count characters, starting at index, and puts part in their place. The new part can be longer or shorter than the old one.
std::string name = "Aria";
name.insert(0, "[DK] ");
std::cout << name << std::endl;
name.erase(0, 5);
std::cout << name << std::endl;

std::string message = "gg!!!!!!";
message.erase(3);
std::cout << message << std::endl;

std::string command = "go north";
command.replace(3, 5, "south");
std::cout << command << std::endl;

This prints:

[DK] Aria
Aria
gg!
go south

The index usually comes from find(). This is how a chat filter hides a rude word:

std::string message = "you are a noob";
int position = message.find("noob");
if (position != std::string::npos) {
    message.replace(position, 4, "****");
}
std::cout << message << std::endl;

This prints:

you are a ****

The check matters. When the word isn't in the message and you pass npos to replace(), the program stops with std::out_of_range: basic_string, just as substr() does with a start past the end.

Instructions

  1. 1

    In place of the first comment, write a function censor with two parameters: the message message, which the function changes, and the word word, which it only reads. Choose how to pass them, as you learned in the lesson on references. The function finds the word in the message and replaces it with as many asterisks as the word has characters; std::string(count, character) makes them. When the word isn't in the message, the function changes nothing. In place of the second comment, call it for each of the three messages with badWord. The first line of the output is then you are such a ****, and the third stays gg, well played. The test also runs your program with another bad word.

  2. 2

    The second message still has one noob in it, because find() finds only the first one. Change the function so that it replaces every occurrence of the word: search again in a loop until find() finds nothing. The second line is then **** team, **** moves.

Back

8/11

  1. 1. Building a String
  2. 2. Part of a String
  3. 3. Searching in a String
  4. 4. Splitting a Command
  5. 5. Quiz: Parts of a String
  6. 6. Alphabetical Order
  7. 7. Uppercase, Lowercase and Digits
  8. 8. Changing a String
  9. 9. Numbers in Strings
  10. 10. Quiz: Buying Potions
  11. 11. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.