Write a method called repl that accepts a String and a number of repetitions as parameters and returns the String concatenated that many times. For example, the call repl("hello", 3) should return "hellohellohello". If the number of repetitions is zero or less, the method should return an empty string.
Program:
public class Ch04Ex02
{
public static void main(String[] args)
{
System.out.println("repl(\"hello\", 0): " + repl("hello", 0));
System.out.println("repl(\"hello\", 1): " + repl("hello", 1));
System.out.println("repl(\"hello\", 3): " + repl("hello", 3));
}
public static String repl(String str, int times)
{
String result = "";
for (int i = 0; i < times; i++)
{
result += str;
}
return result;
}
}
Output:
repl("hello", 0):
repl("hello", 1): hello
repl("hello", 3): hellohellohello