Problem overview
The function initials receives a full name made of a first name and a last name, separated by one space. It returns the first letter of each name, in upper case. The name "grace hopper" gives "GH", even though it is written in lower case. "Ada Lovelace" gives "AL", because the letters are already upper case. A name can be a single letter: "a b" gives "AB".
Approach
The first initial is always the first character of the string. The second one is the first character of the last name, which starts right after the space. So the function needs to know where the space is.
C++ strings have no split method, but find returns the index of a character or a piece of text. Once the index of the space is known, the last name starts one position later. The upper-casing can be done on the whole string at once, so it doesn't matter which letters end up being picked.
Step by step
- Find the space.
fullName.find(" ")returns the index of the first space, which is the only space. The lesson stores it in anintnamedspace. For"grace hopper"it is 5. - Convert the whole name to upper case. The
transformfunction applies::toupperto every character betweenbegin()andend()and writes the results back frombegin(). Upper-casing changes letters but not positions, sospacestill points at the space. - Take the two letters.
fullName.substr(0, 1)is the first letter.fullName.substr(space + 1, 1)is the one letter at the start of the last name. Both are strings of length 1, so+joins them as text.
The solution in C++
class Solution {
public:
string initials(string fullName) {
int space = fullName.find(" ");
transform(fullName.begin(), fullName.end(), fullName.begin(), ::toupper);
return fullName.substr(0, 1) + fullName.substr(space + 1, 1);
}
};
The C++ details that matter:
- How
transformworks. Its first two arguments are the range to read, the third is where to start writing, and the fourth is the function to apply. Writing back into the same string, starting atfullName.begin(), changes it in place. SincefullNameis the function's own copy, the caller's string is not affected. - The
::toupperfunction. It takes a character code and returns its upper-case version. Letters without an upper-case form, and characters that aren't letters, come back unchanged. - Find before transform. The order of the first two lines doesn't change the result, because upper-casing doesn't move any character. Finding the space first just keeps the index obviously valid.
- The two arguments of
substr. They are a start index and a count.substr(space + 1, 1)means one character starting at indexspace + 1. - Why
substrand not[]. The expressionfullName[0]is achar, and adding twocharvalues adds their codes as numbers. Strings of length 1 join as text.
Edge cases and common mistakes
- Taking the letter at the space. The last name starts at
space + 1. Indexspaceis the space character itself. - Forgetting to convert the case. Without
transform,"grace hopper"would give"gh". - Single-letter names. For
"a b"the space is at index 1, the last name starts at index 2, and the result is"AB". No special case is needed. - Names with more than two words. The code only looks at the first space. A name such as a first name, a middle name and a last name would give the middle name's initial. The lesson's input always has exactly two words.
- Missing space. If
findfound no space, it would returnstring::npos, which theintturns into -1. Thenspace + 1is 0 and the function would return the first letter twice. The lesson's names always have one.
Testing
"grace hopper"gives"GH"."Ada Lovelace"gives"AL"."a b"gives"AB".
