This problem combines String
s, functions, and arrays. Super fun!
Write a function called rotateRight
that takes a nullable String
as its first argument and a non-negative
Int
as its second argument and rotates the String
by the given number of characters.
Here's what we mean by rotate:
CS125
rotated right by 1 becomes 5CS12
CS125
rotated right by 2 becomes 25CS1
CS125
rotated right by 3 becomes 125CS
CS125
rotated right by 4 becomes S125C
CS125
rotated right by 5 becomes CS125
CS125
rotated right by 8 becomes 125CS
And so on.
Notice how characters rotated off the right end of the String
wrap around to the left.
This problem is tricky! Here are a few hints:
String
to an array of characters before you begin.characters
back into a String
like this: String(characters)
.substring
.If the passed String
argument is null
, you should return null
.
Good luck and have fun!
Stuck? You may find these lessons helpful: