String Rotate Right
Geoffrey Challen // 2020.9.1
This problem combines Strings, functions, and arrays. Super fun!
Write a function called rotateRight that takes a 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:
CS125rotated right by 1 becomes5CS12CS125rotated right by 2 becomes25CS1CS125rotated right by 3 becomes125CSCS125rotated right by 4 becomesS125CCS125rotated right by 5 becomesCS125CS125rotated right by 8 becomes125CS
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:
- You will want to use the Java remainder operator to perform modular arithmetic, so please review the remainder operator.
- You will probably want to convert the
Stringto an array of characters before you begin. - You can convert an array of characters
charactersback into aStringlike this:new String(characters). - You can also solve this problem quite elegantly using
substring.
If the passed String argument is null, you should return null.
Good luck and have fun!