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
- Get the last character. For
"banana", the length is 6, the last index is 5, ands[5]is'a'. - Repeat it. Pass that character and
ntonew string. Withnequal to 3, the result is"aaa". - 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 achar. - Using
s.Lengthas the index. The last valid index is one less than the length.s[s.Length]throws anIndexOutOfRangeException. - 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
| s | n | Result |
|---|---|---|
"banana" | 3 | "aaa" |
"Wow!" | 2 | "!!" |
"z" | 1 | "z" |
