logo CodeStepByStep logo

repeat_string

Language/Type: PHP recursion

Write a recursive function named repeat_string that accepts a String $str and an integer $n as parameters and that returns $str concatenated $n times. For example, the call of repeat_string("hello", 3) returns "hellohellohello". Your function should throw an Exception if passed any negative value for $n.

Constraints:

  • Do not declare any global variables.
  • Do not use any loops; you must use recursion.
  • Do not use any auxiliary data structures (e.g. arrays) or strings to solve this problem.
  • You are allowed to define other "helper" functions if you like; they are subject to these same constraints.
Type your PHP solution code here:


This is a function exercise. Write a PHP function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve an exercise or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect tests, etc.), please

Is there a problem? Contact a site administrator.

©, all rights reserved.