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

Alphabetical Order

You know that == and != check whether two strings are exactly the same. Strings can also be compared with <, >, <= and >=, and then a < b means that a comes before b in alphabetical order:

std::string first = "Orc";
std::string second = "Goblin";
if (first < second) {
    std::cout << first << " comes first" << std::endl;
} else {
    std::cout << second << " comes first" << std::endl;
}

This prints:

Goblin comes first

C++ compares two strings character by character from the start, and the first character that differs decides. In the computer, every character is stored as a number, its code, and the character with the smaller code comes first. The letters have codes in the order of the alphabet: 'A' is 65, 'B' is 66, and so on up to 'Z', which is 90. The lowercase letters come after them, from 'a', 97, to 'z', 122. That has a few results you might not expect:

  • Every uppercase letter comes before every lowercase one, so "Troll" < "bat" is true.
  • When a string is the start of a longer one, the shorter one comes first: "Bat" < "Batman" is true.
  • Digits and the space have smaller codes than letters, so they come before them.
  • A letter with an accent takes two chars with big codes, so it comes after z. Keep the strings you compare without accents.

In the exercise, you go through a vector of strings. You can write the loop as for (const std::string& item : inventory). Just like a const reference parameter, item then reads each element without copying it, and can't change it. That's the usual way to write a loop that only reads strings.

Instructions

  1. 1

    The vector inventory holds the items in the hero's backpack. Find the item that comes first in alphabetical order, in the same way as you looked for the largest value in the Loops lesson, only with <. Print it: First: Key. The test also runs your program with other items, so keep the line with the vector as it is.

  2. 2

    Is the backpack in alphabetical order? Go through the vector with an index loop and compare each item with the next one. When an item is greater than the one after it, the order is wrong. After the loop, print Sorted or Not sorted, once.

Back

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