Q:

Write a PHP program to create a new string taking the first character from a given string and the last character from another given string

0

Write a PHP program to create a new string taking the first character from a given string and the last character from another given string. If the length of any given string is 0, use '#' as its missing character

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function test($s1, $s2)
{ 
             $lastChars = "";

            if (strlen($s1) > 0)
            {
                $lastChars = $lastChars.substr($s1, 0, 1);
            }
            else
            {
                $lastChars = $lastChars."#";
            }

            if (strlen($s2) > 0)
            {
                $lastChars = $lastChars.substr($s2, strlen($s2) - 1);
            }
            else
            {
                $lastChars = $lastChars."#";
            }

            return $lastChars;
    }


echo test("Hello", "Hi")."\n";
echo test("Python", "PHP")."\n";
echo test("JS", "JS")."\n";
echo test("Csharp", "")."\n";

Sample Output:

Hi
PP
JS
C#

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now