Skip to main content

Section 9.11 Operators for Strings

Subsection 9.11.1 Comparisons

Strings support some of the same operators as numbers. For example, we can use the comparison operators to compare strings:
Listing 9.11.1.
The equality tests are straightforward. "apple" is the same as itself, but not the same as "Banana". Looking at the comparison between "apple" and "Apple", it is clear that comparisons are case sensitiveβ€”they care about the difference between a and A.
The β€œless than” tests are a little less obvious. But they make sense when you recall that characters have numeric values based on their position in the ASCII table (or Unicode table for wide characters). Why is "Apple" less than "Banana"? Because A comes before B on the ASCII table. Why is "Banana" less than "apple"? Because B comes before a on the ASCII table. If the first n characters of two strings match, the n + 1 characters will determine their order "pear" < "peer" is true because both start with pe and the third character in pear - a - is less than the 3rd character in peer - e`.

Insight 9.11.1.

Because string comparisons depend on case, it is common to convert strings to all lower or all upper case before comparing them.

Subsection 9.11.2 Concatenation

Strings also support the + operator. But for strings + does not mean β€œplus”. It means concatenate, which is a fancy way of saying β€œjoin end-to-end”. So "Hello, " + "World!" yields the string "Hello, World!".
+ and the += shortcut is useful if you want to build up a string piece by piece.
Listing 9.11.2.
string name = "Ada";
name = name + " Lovelace"; // name is now "Ada Lovelace"
string greeting = "Hello, " + name + "!";
                           // greeting is now "Hello, Ada Lovelace!"

string name2 = "Alan";
name2 += " Turing";        // name2 is now "Alan Turing"
Many string algorithms involve reading one string and building another. For example, to reverse a string, we can concatenate one character at a time to the front of a new string. The initial value of reversed is "", which is an empty string. As we iterate through each character, we set reversed to be that character concatenated with the other characters we have seen. Since the loop goes from beginning to end, adding each character to the front of reversed means that the last character of myString ends up being the first character of reversed:
Listing 9.11.3.

Checkpoint 9.11.1.

What would the result of the following comparison be? Where 1 means true and 0 means false.
"bread" < "bread";
  • They are equal so one can’t be greater than the other.
  • Correct! because they are equal. They are equal because all characters match.

Checkpoint 9.11.2.

What would the result of the following comparison be? Where 1 means true and 0 means false.
"car" == "Car";
  • Remember that case matters.
  • Correct! The case of the first characters are different.

Checkpoint 9.11.3.

What would the result of the following comparison be? Where 1 means true and 0 means false.
"Dog" < "Doghouse";
  • Both match up to the g but Dog is shorter than Doghouse so it comes first in the dictionary.
  • Strings are compared character by character.

Checkpoint 9.11.4.

What would the result of the following comparison be? Where 1 means true and 0 means false.
"dog" < "Dog";
  • d is greater than D
  • Yes, upper case is less than lower case according to the ordinal values of the characters.
  • They are the same word
  • C++ is case sensitive meaning that upper case and lower case characters are different.

Warning 9.11.2.

The + operator is defined for a char and a C-string, but it does not do what you expect. Avoid trying to write something like "hell" + 'o'. Make sure when you are using + you are using a variable of type string and not a string literal.

Checkpoint 9.11.5.

Put together the code below to create a function greeter that adds β€œHello” before a message and β€œ. goodbye.” after it behind and then prints the new message. Example: greeter("Bob") will print β€œHello Bob. goodbye.”
You have attempted of activities on this page.