Assume we have already defined a variable of type String called myString with the following line of code: String myString = "???"; // 'myString' can have any String value The i-th rotation of a string is the result of shifting every character of the string i positions to the right, with characters at the end wrapping around to the beginning. For example, the 2nd rotation of the string "Niema" is "maNie". For any given string, the 0-th rotation is simply itself. TASK: Print every rotation of myString, one rotation per line, starting with the 0-th ro

Respuesta :

Answer: You can use this code for doing that.

public static void main (String[] args) throws java.lang.Exception {

   String myString = "Something";

   for(int i = 0; i < s.length(); i++){

       myString = shift(myString);

       System.out.println(myString);

   }

}

/**Method string for the cycle

public static String shift(String s) {

   return s.charAt(s.length()-1)+s.substring(0, s.length()-1);

}

Explanation: The method "shift" is the key. It returns a string, and the code line into it does the following:

return s.charAt(s.length()-1) : "cuts" the last character of the myString value.

Then concatenating (+) with

s.substring(0, s.length()-1) : creates a substring with the "rest" of the string.

The for loop repeats the process, assigning the method result to the value "myString" again and again, until it reaches the string length.

The output:

Something

gSomethin

ngSomethi

ingSometh

hingSomet

thingSome

ethingSom

methingSo

omethingS

Something

Hope it helps!