Insight 9.9.1.
Because string comparisons depend on case, it is common to convert strings to all lower or all upper case before comparing them.
"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.
"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.
"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.
+ 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.
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"
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:
1 means true and 0 means false.
"bread" < "bread";
1 means true and 0 means false.
"car" == "Car";
1 means true and 0 means false.
"Dog" < "Doghouse";
1 means true and 0 means false.
"dog" < "Dog";
+ 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.
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.β