Q:

Write a PHP program to create a new string from a given string after swapping last two characters

0

Write a PHP program to create a new string from a given string after swapping last two characters

All Answers

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

<?php
function test($s1)
{ 
        if (strlen($s1) > 1)
            {
                return substr($s1, 0, strlen($s1) - 2) . substr($s1, strlen($s1)-1, 1) . substr($s1, strlen($s1)-2, 1);
            }
            else
            {
                return $s1;
            }
    }

echo test("Hello")."\n";
echo test("Python")."\n";
echo test("PHP")."\n";
echo test("JS")."\n";
echo test("C")."\n";

Sample Output:

Helol
Pythno
PPH
SJ
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