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

Summary

In this lesson, you learned that:

  • A value of type std::string is called a string. += adds a string or a single char to its end, std::string(5, '#') creates a string of five #, and empty() tells you whether a string has no characters.
  • text.substr(start, length) returns a new string: length characters from the index start. Without the length, the part goes to the end. A length past the end takes the characters there are, but a start past the end stops the program with std::out_of_range.
  • text.find(part) returns the index where a string or a character first occurs, and text.find(part, from) starts searching at the index from. When it finds nothing, it returns std::string::npos. Compare the result with std::string::npos; on its own, it isn't a yes or a no.
  • With find() and substr(), you split a string, for example a command at the space: the part before it is substr(0, space), the part after it substr(space + 1). Check first that the space is there.
  • < and > compare strings in alphabetical order, by the codes of their characters. Uppercase letters come before lowercase ones, and a string comes before a longer one that starts with it.
  • for (const std::string& item : inventory) reads the strings in a vector without copying them, and for (char& c : text) changes the characters of a string.
  • <cctype> works with single characters. std::toupper() and std::tolower() return a changed character; store it in a char, because printed straight away, it's a number. std::isupper(), std::isalpha() and std::isdigit() answer a question, so you use them as conditions.
  • insert(), erase() and replace() change the string itself. Together with find(), they replace words, as in a chat filter.
  • std::stoi() and std::stod() turn a string into an int or a double, and std::to_string() turns a number into a string. A string that doesn't start with a number stops the program, so check it before you convert it.
  • Strings that you measure, cut, search or compare should have no letters with accents, because such a letter takes two chars.

In the next lesson, Structs, you will create a type of your own, which keeps a monster's name, health and attack together in one variable.

Instructions

  1. 1

    Let's practice with a text adventure once more. Above main, write a function healthBar with the parameters health and maxHealth, which returns the health bar as a string, as in the first exercise of this lesson. In main, print Before: and the bar: Before: [####------]. The test also runs your program with other values.

  2. 2

    Below that, carry out the command: split it at the space into the verb and the number. When the verb is heal, add the number to health, but never above maxHealth. Any other verb only prints Unknown command: and the verb. Then print After: and the bar again: After: [#######---]. The test also runs your program with other commands.

Back

11/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.