Start learning free

LESSON 74 OF 667 · STRINGS - CHALLENGES

Initials in C++: find, transform and substr

Practice Read the full explanation

Given a person's full name fullName, made of a first name and a last name, return their initials: the first letter of each name, in upper case.

The two names are separated by a single space. The initials are always upper case, even when the name is written in lower case.

 

Example 1:

Input: fullName = "grace hopper"
Output: "GH"
Explanation: The first name starts with g and the last name with h. In upper case, they give "GH".

Example 2:

Input: fullName = "Ada Lovelace"
Output: "AL"
Explanation: A and L are already upper case, so they stay as they are.

Example 3:

Input: fullName = "a b"
Output: "AB"
Explanation: A name can be a single letter: that letter is its initial.

 

Constraints:

  • fullName has exactly two words, the first name and the last name, separated by one space.
  • Both names are made of English letters only, lower case or upper case.
STEP 1 OF 3
    Ctrl + Enter to check

    C++ · STRINGS - CHALLENGES

    Initials in C++, explained

    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

    1. Find the space. fullName.find(" ") returns the index of the first space, which is the only space. The lesson stores it in an int named space. For "grace hopper" it is 5.
    2. Convert the whole name to upper case. The transform function applies ::toupper to every character between begin() and end() and writes the results back from begin(). Upper-casing changes letters but not positions, so space still points at the space.
    3. 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 transform works. 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 at fullName.begin(), changes it in place. Since fullName is the function's own copy, the caller's string is not affected.
    • The ::toupper function. 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 index space + 1.
    • Why substr and not []. The expression fullName[0] is a char, and adding two char values 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. Index space is 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 find found no space, it would return string::npos, which the int turns into -1. Then space + 1 is 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".
    Keep going with C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free