Java
Write the method rightN. The method has two inputs: a String str and an int n. The output of the method is a new String that contains a "rotated right n" version of str. You can assume that the length of str will be at least n. Here is some sample input and output:
// TestCode.java public class TestCode { public static String rightN(String s, int n){ return s.substring(s.length()-n)+s.substring(0,s.length()-n); } public static void main(String args[]) { System.out.println(rightN("hello",2)); System.out.println(rightN("Chocolate",3)); System.out.println(rightN("Chocolate",1)); } }
public String rightN(String s, int n){ return s.substring(s.length()-n)+s.substring(0,s.length()-n); }
Get Answers For Free
Most questions answered within 1 hours.