Join, compare, and find text
Learn content comparison, lexicographical order, substring counts, and the not-found sentinel.
Joining is explicit
For strings, + joins text and += appends to the left-hand string. Spaces are not inserted automatically: first + " " + second explicitly supplies one. Character arithmetic such as 'a' + 'b' does not produce the string "ab".
String equality compares complete content, including case, spaces, order, and length. "Code" differs from "code", and "hi" differs from "hi ". Unlike raw arrays, ordinary string assignment and comparison operate on the whole value.
Compare at the first difference
Lexicographical comparison checks from the left and uses the first different character. If one string ends while all its characters match the other’s beginning, the shorter comes first. For lowercase ASCII words, this follows the familiar alphabet.
"car" comes before "cat"; "app" comes before "apple"; "aa" comes before "z" despite being longer. Numeric-looking text is still text: "10" comes before "2" because '1' precedes '2'.
A substring uses a start and a count
For "banana", substr(1, 3) returns "ana". The second argument counts characters, rather than naming the final index. An inclusive interval [l, r] needs r − l + 1 characters; the half-open interval [l, r) needs r − l.
substr(pos) returns the remaining suffix. A count beyond the remaining length is shortened. pos equal to size() returns an empty string; a position beyond size() is an error. substr does not modify the original string.
A match at zero is still a match
find returns the first starting position or std::string::npos when absent. Use auto to preserve the result’s actual size type, then compare explicitly with npos. if (s.find(pattern)) is wrong: a valid match at zero becomes false.
In banana, find("ana") returns 1 while find("ana", 2) returns 3. One call does not count all matches. Repeated or overlapping matches require a deliberate next search position.
#include <iostream>
#include <string>
int main() {
std::string s = "banana";
std::cout << s.substr(1, 3) << '\n';
auto position = s.find("ana");
if (position != std::string::npos) { std::cout << position << '\n'; }
auto missing = s.find("xyz");
if (missing == std::string::npos) { std::cout << "NOT FOUND\n"; }
std::cout << s << '\n';
return 0;
}For s = "training", what does s.substr(2, 4) return?
Turn the idea into code
Read two nonempty lowercase ASCII words and print the lexicographically smaller one. If they match, print that word once.
Give me a hint
Compare the strings directly, rather than comparing only their lengths.
Show the explained solution
Use a <= b to choose the first string when it is smaller or equal; otherwise print b. No sorting algorithm is necessary for this two-value decision.
#include <iostream>
#include <string>
int main() {
std::string a, b;
std::cin >> a >> b;
if (a <= b) { std::cout << a << '\n'; }
else { std::cout << b << '\n'; }
return 0;
}Your notes
Mark complete when you can explain it yourself.