Start learning free

LESSON 69 OF 281 · STRINGS - CHALLENGES

Creating IDs in C++: Taking Pieces of Two Strings

Practice Read the full explanation

A website creates a user ID from each person's name: the first 2 letters of the first name, followed by the last 3 letters of the last name.

Given the first name first and the last name last, return the ID.

 

Example 1:

Input: first = "John", last = "Smith"
Output: "Joith"
Explanation: "Jo" are the first 2 letters of "John" and "ith" are the last 3 letters of "Smith".

Example 2:

Input: first = "Jo", last = "Doe"
Output: "JoDoe"
Explanation: "Jo" has exactly 2 letters and "Doe" exactly 3, so both names are used whole.

Example 3:

Input: first = "ada", last = "Lovelace"
Output: "adace"
Explanation: The letters keep their case: the ID doesn't change any letter to upper or lower case.

 

Constraints:

  • first has at least 2 letters and last has at least 3 letters.
  • Both names are made of English letters only, lowercase or uppercase.
STEP 1 OF 1
    Ctrl + Enter to check

    C++ · STRINGS - CHALLENGES

    Creating Ids in C++, explained

    Problem overview

    The function creatingIds builds a user ID from a first name and a last name. The ID is the first 2 letters of first, followed by the last 3 letters of last.

    For "John" and "Smith" the ID is "Joith": "Jo" comes from the start of John, and "ith" from the end of Smith. When a name has exactly the number of letters needed, it is used whole, so "Jo" and "Doe" give "JoDoe". Letters keep their case: "ada" and "Lovelace" give "adace", with no capital added.

    Approach

    Each half of the ID is a piece of one string, and the two pieces are joined. The first piece is at the beginning of first, so it starts at index 0 and has a known length, 2. The second piece is at the end of last. Its length is known too, 3, but its starting index depends on how long the name is.

    The last 3 letters of a string of length L start at index L - 3. For "Smith", the length is 5, so the piece starts at index 2 and is "ith". The same rule works for any name with at least three letters, which the constraints guarantee.

    Step by step

    1. Take the first 2 letters of first. first.substr(0, 2) starts at index 0 and takes 2 characters. The second argument of substr is how many characters to keep.
    2. Take the last 3 letters of last. last.substr(last.size() - 3) starts at the length minus 3. With no second argument, substr runs to the end of the string, so no count is needed.
    3. Join the pieces with + and return the result.

    The solution in C++

    class Solution {
    public:
        string creatingIds(string first, string last) {
            return first.substr(0, 2) + last.substr(last.size() - 3);
        }
    };
    

    C++ details worth knowing here:

    • Two forms of substr. substr(pos, count) takes at most count characters from pos, and substr(pos) takes everything from pos to the end. On "John", substr(0, 2) is "Jo" and substr(2) is "hn".
    • Counting from the end. C++ strings have no negative indexes, so the start of "the last 3 letters" has to be computed from size().
    • size() is unsigned. It returns an unsigned type, so last.size() - 3 wraps around to a huge number if last has fewer than 3 letters. The constraints rule that out here.
    • Nothing is changed in place. substr returns a new string each time, and the original names are left as they were.

    Edge cases and common mistakes

    • Reading the second argument as an end index. In first.substr(0, 2) the 2 is a count: two characters starting at index 0, which are the characters at indexes 0 and 1.
    • Using the wrong string's length. The start of the second piece must use last.size(), not first.size(). The two names usually have different lengths.
    • Names of exactly the minimum length. With "Jo" and "Doe", the call last.substr(0) returns "Doe" and first.substr(0, 2) returns "Jo". Both pieces are the whole names, and no special case is needed.
    • A last name that is too short. With a last name of 2 letters, last.size() - 3 wraps around and substr throws std::out_of_range. The lesson's inputs never have such a name.
    • Changing the case. The ID uses the letters exactly as given. Don't capitalise or lower-case anything.

    Testing

    • "John" and "Smith" give "Joith".
    • "Jo" and "Doe" give "JoDoe".
    • "ada" and "Lovelace" give "adace".
    Keep going with C++1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free