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`.
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!".
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:
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.
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.β