Write a PHP program to create a new string which is n (non-negative integer) copies of the the first 3 characters of a given string

  • برمجة بي اتش بي

Write a PHP program to create a new string which is n (non-negative integer) copies of the the first 3 characters of a given string. If the length of the given string is less than 3 then return n copies of the string

الأجوبة

<?php
function test($s, $n)
{
    $result = "";
    $frontOfString = 3;

    if ($frontOfString > strlen($s))
        $frontOfString = strlen($s);

       $front = substr($s, 0, $frontOfString);

    for ($i = 0; $i < $n; $i++)
         {
            $result = $result.$front;
        }
     return $result;
 }


echo test("Python", 2)."\n";
echo test("Python", 3)."\n";
echo test("JS", 3)."\n";

Sample Output:

PytPyt
PytPytPyt
JSJSJS
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...