Problem overview
The function repeatLastChar receives a string s and a whole number n from 1 to 10. It returns a new string made of the last character of s, written n times in a row.
For s = "banana" and n = 3 the last character is a, so the result is "aaa". The last character can be any character: "Wow!" with n = 2 gives "!!". A one-character string such as "z" with n = 1 gives "z".
Approach
The task splits into two small jobs: find the last character, then repeat it. A loop that appends to a string would work, but the standard library already has the second job covered. strings.Repeat(text, n) returns text written n times in a row, so strings.Repeat("ab", 3) is "ababab".
For the first job, the last index of a string is len(s)-1, because indexes start at 0. Reading s[len(s)-1] gives that character as a byte, a number. strings.Repeat wants a string as its first argument, so the byte must be converted with string(...) first.
Step by step
- Read the last byte with
s[len(s)-1]. - Convert it to a one-character string with
string(...). - Repeat it with
strings.Repeat(..., n). - Return the result.
The solution in Go
func repeatLastChar(s string, n int) string {
return strings.Repeat(string(s[len(s)-1]), n)
}
Running it on the three examples gives these results:
s = "banana", n = 3 -> "aaa"
s = "Wow!", n = 2 -> "!!"
s = "z", n = 1 -> "z"
Details to keep in mind:
- A byte is not a string.
s[len(s)-1]has the typebyte. Passing it straight tostrings.Repeatis a compile error, because the function expects astring. The conversionstring(...)fixes that. - The
stringspackage.Repeatlives there. In a file of your own, the program needsimport "strings"at the top; in the lesson's editor that import is already in place for you. - Counting bytes. Indexing a string reads bytes. For English letters, digits and punctuation, one character is one byte, which is what the constraints promise. A character such as
étakes two bytes, and its last byte alone isn't a valid character. - A negative count panics.
strings.Repeat("r", -1)stops the program with "strings: negative Repeat count". The constraints keepnbetween 1 and 10, so this can't happen here.
The same thing with a loop
Without strings.Repeat, a loop that adds the character n times does the job:
func lastRepeat(s string, n int) string {
last := string(s[len(s)-1])
result := ""
for i := 0; i < n; i++ {
result += last
}
return result
}
Called with "banana" and 3, it also returns "aaa". Naming the last character once, in last, avoids repeating the index expression. The one-line version with strings.Repeat says the same thing with less to read.
Edge cases and common mistakes
- Repeating the whole string.
strings.Repeat(s, n)gives"bananabananabanana"for the first example. Only the last character is repeated. - Using
len(s)as the index. The last valid index islen(s)-1. Indexing atlen(s)stops the program with an "index out of range" panic. - Taking the first character.
s[0]would give"bbb"for"banana". - One-character strings. The last index is 0, the same as the first, so
"z"works with no special case. - Punctuation. The last character isn't always a letter. For
"Wow!"it is the exclamation mark, and the function repeats it just the same.
Testing
"banana", 3 gives"aaa"."Wow!", 2 gives"!!"."z", 1 gives"z".
