Start learning free

LESSON 67 OF 667 · STRINGS - CHALLENGES

Concat First and Last Char in C++: Using substr

Practice Read the full explanation

Given a string s, return a new string made of two characters: the first character of s, followed by its last character.

 

Example 1:

Input: s = "hello"
Output: "ho"
Explanation: The first character is "h" and the last one is "o".

Example 2:

Input: s = "Hello World!"
Output: "H!"
Explanation: Every character counts, so the last one is the "!", not the "d". The "H" stays upper case.

Example 3:

Input: s = "ab"
Output: "ab"
Explanation: With only two characters, the first is "a" and the last is "b".

 

Constraints:

  • s has at least 2 characters.
  • s contains English letters, digits, spaces and punctuation, and doesn't start or end with a space.
STEP 1 OF 1
    Ctrl + Enter to check

    C++ · STRINGS - CHALLENGES

    Concat First And Last Char in C++, explained

    Problem overview

    The function concatFirstAndLastChar receives a string s with at least two characters. It returns a new string of exactly two characters: the first character of s, followed by its last one.

    For "hello" the answer is "ho". Every character counts, so for "Hello World!" the last character is the exclamation mark and the result is "H!". The case of a letter never changes. A two-character string such as "ab" comes back unchanged, because its first and last characters are the whole string.

    Approach

    The task has two small parts: pick out the first character, pick out the last one, and glue them together. In C++ the natural tool for picking out a piece of a string is substr. It returns a new std::string and leaves the original alone.

    The first character sits at index 0. The last one sits at index s.size() - 1, because indexes start at 0 and a string of length 5 has indexes 0 to 4. Taking one character from each place gives two one-character strings, and + joins two strings into one.

    Step by step

    1. Take the first character as a string. substr(pos, count) starts at pos and takes at most count characters, so s.substr(0, 1) is a string holding just the first character.
    2. Take the last character the same way. The call s.substr(s.size() - 1) starts at the last index. With no second argument, substr runs to the end of the string, and from the last index that is exactly one character.
    3. Join the two pieces with + and return the result.

    The solution in C++

    class Solution {
    public:
        string concatFirstAndLastChar(string s) {
            return s.substr(0, 1) + s.substr(s.size() - 1);
        }
    };
    

    A few C++ details explain why the code is written this way:

    • Why substr and not s[0]. The expression s[0] is a single char, which is a small integer type. Adding two char values adds their character codes as numbers: 'h' + 'o' is the int 215, not the text "ho". Two std::string values, on the other hand, join as text.
    • The second argument is a count. In substr(0, 1) the 1 means "take one character". It is not the index where the piece stops.
    • The result is a new string. The parameter s is passed by value, so the function works on its own copy, and the caller's string is untouched either way.
    • size() is unsigned. s.size() has an unsigned type, so on an empty string s.size() - 1 wraps around to a huge number. Here the constraints guarantee at least two characters, so this doesn't happen.

    Edge cases and common mistakes

    • Using + on two char values. Writing s[0] + s[s.size() - 1] adds two numbers and gives an int, which the function can't return as a string: the code doesn't compile. Use substr to get strings.
    • Off by one on the last index. s.substr(s.size()) is legal and returns an empty string, so the result would be just the first character. The last character starts at s.size() - 1.
    • A string of exactly two characters. "ab" gives "a" plus "b", which is "ab". The two pieces never overlap, and the code needs no special case.
    • An empty string. Calling substr with a start position beyond the end of the string throws std::out_of_range. On an empty string, s.size() - 1 wraps to a huge number and the second substr call throws. The lesson's inputs never have fewer than two characters.

    Testing

    • "hello" gives "ho".
    • "Hello World!" gives "H!".
    • "ab" 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