[Solved] Repeat String - PHP Function Task

  

5
Topic starter

Write a function that receives a string and a repeat count n. The function should return a new string:

Input:

  • abc
  • 3

Output:

abcabcabc

Input 2:

  • String
  • 2

Output 2:

StringString

1 Answer
4

My PHP function solution:

<?php
 
$string = readline();
$repeat = readline();
 
echo repeatString($string, $repeat);
 
function repeatString($s, $r) {
    $newString = '';
    for ($i = 0; $i < $r; $i++) {
        $newString .= $s;
    }
    return $newString;
}
Share: