Answer:
public class RepeatedString
{
public static void main(String[] args) {
System.out.println(repeatString("apple", 3));
}
public static String repeatString(String str, int n) {
String newString = str.substring(str.length()-n);
System.out.println(newString);
String ns = "";
for(int i=0; i<n; i++) {
ns += newString;
}
return ns;
}
}
Explanation:
- Create a function called repeatString that takes two parameter
- Get the last n characters of the string using substring function, and assign it to the newString variable
- Initialize an empty string to hold the repeated strings
- Initialize a for loop that iterates n times
- Inside the loop, add the repeated strings to the ns
- When the loop is done, return ns
- Inside the main, call the function