Start learning free

LESSON 71 OF 289 · STRINGS - CHALLENGES

Repeat Last Char in C#: Building a String with new string

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 method RepeatLastChar receives a string s and a number n, and returns a new string made of the last character of s written n times in a row. For "banana" and 3 the result is "aaa". For "Wow!" and 2 it is "!!": the last character can be a letter, a digit, a space or punctuation. For "z" and 1 it is "z", because a one-character string has its last character at index 0.

    Approach: find the character, then repeat it

    The task has two parts. First find the last character. Indexes in a C# string start at 0, so the last one is at s.Length - 1, and s[s.Length - 1] is a value of type char. Second, make a string that contains that character n times.

    You could build the answer with a loop that appends one character per turn. C# has a shorter way for exactly this case: a string constructor that takes a character and a count.

    The constructor new string(char, int)

    new string('x', 3) creates the string "xxx". The character comes first and the count second. The first argument has to be a char, written in single quotes, or a variable of that type. A string such as "x" does not fit, and the compiler reports that it cannot convert from string to char.

    Console.WriteLine(new string('-', 5));
    Console.WriteLine("[" + new string('x', 0) + "]");
    -----
    []

    A count of 0 gives an empty string. A negative count throws an ArgumentOutOfRangeException, but the lesson guarantees that n is between 1 and 10.

    Step by step

    1. Get the last character. For "banana", the length is 6, the last index is 5, and s[5] is 'a'.
    2. Repeat it. Pass that character and n to new string. With n equal to 3, the result is "aaa".
    3. Return it. There is nothing else to combine, so the new string is the return value.

    The solution in C#

    class Solution {
        public string RepeatLastChar(string s, int n) {
            return new string(s[s.Length - 1], n);
        }
    }

    The expression s[s.Length - 1] is already a char, so it can go straight into the constructor without any conversion. The original string s is not changed: strings in C# are immutable, and new string makes a fresh one.

    The constructor is also an answer to "how do I turn one char into a string". Starting from an empty string and adding a character with + works too, but here the count makes the constructor the better fit.

    Edge cases and common mistakes

    • Swapping the arguments. new string(n, s[s.Length - 1]) does not compile: the first argument must be the character.
    • Using double quotes for the character. new string("a", 3) is rejected, because "a" is a string and the constructor wants a char.
    • Using s.Length as the index. The last valid index is one less than the length. s[s.Length] throws an IndexOutOfRangeException.
    • Repeating the whole string. The lesson repeats only the last character: "banana" with 3 gives "aaa", not "bananabananabanana".

    Notice that the method does not need to know whether the last character is a letter, a digit or a symbol. A char is a char, and new string repeats it in the same way. That is why the second example, with the exclamation mark, needs no special treatment. The only thing that varies is the input, not the code.

    Testing

    snResult
    "banana"3"aaa"
    "Wow!"2"!!"
    "z"1"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