Start learning free

LESSON 71 OF 281 · STRINGS - CHALLENGES

Repeat Last Char in C++: Building a String from One Character

Practice Read the full explanation

Given a string s and a number n, return a new string made of the last character of s, written n times in a row.

 

Example 1:

Input: s = "banana", n = 3
Output: "aaa"
Explanation: The last character of "banana" is "a". Written 3 times, it gives "aaa".

Example 2:

Input: s = "Wow!", n = 2
Output: "!!"
Explanation: The last character can be any character: here it is the exclamation mark.

Example 3:

Input: s = "z", n = 1
Output: "z"
Explanation: A string of one character: its last character is "z", written once.

 

Constraints:

  • s has at least one character and doesn't start or end with a space.
  • s contains English letters, digits, spaces and punctuation.
  • n is a whole number from 1 to 10.
STEP 1 OF 1
    Ctrl + Enter to check

    C++ · STRINGS - CHALLENGES

    Repeat Last Char in C++, explained

    Problem overview

    The function repeatLastChar receives a string s and a number n. It returns a new string made of the last character of s, written n times in a row.

    For "banana" and n = 3 the last character is 'a', so the answer is "aaa". The last character can be anything: for "Wow!" and n = 2 it is the exclamation mark, and the answer is "!!". For "z" and n = 1 the string has one character, which is both first and last, so the answer is "z". The number n is between 1 and 10.

    Approach

    There are two jobs. First, find the last character. Second, build a string that holds n copies of it. The second job needs no loop, because the std::string class has a constructor that does exactly that.

    Indexes start at 0, so a string of length 6 has indexes 0 to 5, and the last character is at index s.size() - 1. Indexing with [] gives a single char, which is what the constructor wants.

    Step by step

    1. Get the last character. s[s.size() - 1] is the char at the last index.
    2. Build the repeated string. string(n, c) creates a string of n copies of the character c. The count comes first and the character second.
    3. Return it. The whole function is one expression, so the constructor call is the returned value.

    The solution in C++

    class Solution {
    public:
        string repeatLastChar(string s, int n) {
            return string(n, s[s.size() - 1]);
        }
    };
    

    The details that matter in C++:

    • The fill constructor. string(3, 'x') is "xxx". It builds a new string directly, with no loop and no repeated +=.
    • Argument order. Swapping the arguments, as in string('x', 3), still compiles, because a char converts to a number and the number 3 converts to a char. It builds a string of 120 copies (the character code of 'x') of the character with code 3, which is a control character, not a letter. Count first, then the character.
    • char versus string. s[i] is a char, written with single quotes when it is a literal, like 'r'. A string literal uses double quotes. The fill constructor needs a char.
    • An alternative. s.back() returns the last character too, and it reads more clearly than s[s.size() - 1]. Both need a non-empty string.
    • Empty strings. Indexing an empty string at size() - 1 reads far outside the string, because size() - 1 wraps around to a huge unsigned number. The lesson's strings always have at least one character.

    Edge cases and common mistakes

    • Using index s.size(). The last valid index is one less than the length. Index 6 in "banana" is not a letter.
    • Taking the first character by mistake. s[0] for "banana" is 'b', which would give "bbb".
    • Adding characters. Repeating with s[i] + s[i] adds two character codes as numbers instead of joining text. The fill constructor avoids that trap.
    • Using n as the index. n is how many copies to write, and it has nothing to do with the position of the character.
    • A single-character string. "z" with n = 1 gives "z". The last index is 0, and the code works as it is.

    Testing

    • "banana" and 3 give "aaa".
    • "Wow!" and 2 give "!!".
    • "z" and 1 give "z".
    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