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

Splitting a Command

Together, find() and substr() split a string into parts: first find where the parts meet, then take the part before that place and the part after it. In a full name, the parts meet at the space:

std::string name = "Aria Stormwind";
int space = name.find(' ');
std::string firstName = name.substr(0, space);
std::string lastName = name.substr(space + 1);
std::cout << firstName << std::endl;
std::cout << lastName << std::endl;

This prints:

Aria
Stormwind

The space is at index 4. The first name is the part from index 0 with the length 4, which is everything before the space. The last name starts one character after the space and goes to the end. Mind the + 1: without it, the last name would start with the space.

Old text adventure games were played exactly like this. The player types a command such as take sword or go north, and the game splits it at the first space into a verb, what to do, and an object, what to do it with. Everything after the first space belongs to the object, so in drink red potion, the verb is drink and the object red potion.

What if there is no space? Run the same code with the name "Aria", and it prints:

Aria
Aria

There's no error, but the result is wrong. find() found no space, so space holds -1. The last name then starts at index -1 + 1, which is 0, and substr() takes -1 as the biggest possible length, which means up to the end. So when a string may have no space, check the result of find() before you split it.

Instructions

  1. 1

    Split command at the space into the verb and the object, and print them on two lines: Verb: take and Object: lantern. The test also runs your program with other commands.

  2. 2

    Some commands are a single word, such as look. Then the whole command is the verb, and there is no object. Change the program so that for a command without a space, it prints only Verb: look, without the line with the object. The test runs your program with "look" too. (To try it yourself, change the command, run the program, and then change it back to "take lantern".)

Back

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